JasonGenX
JasonGenX

Reputation: 5434

Qt: How to draw a dummy line edit control

I have a QPainter, and a rectangle.

i'd like to draw a QLineEdit control, empty. Just to draw it, not to have a live control. How do I do that? I have tried QStyle::drawPrimitive to no avail. nothing gets drawn.

 QStyleOption option1;
 option1.init(contactsView); // contactView is the parent QListView
 option1.rect = option.rect; // option.rect is the rectangle to be drawn on.
 contactsView->style()->drawPrimitive(QStyle::PE_FrameLineEdit, &option1, painter, contactsView);

Naturally, i'd like the drawn dummy to look native in Windows and OSX.

Upvotes: 2

Views: 2550

Answers (1)

Dave Mateer
Dave Mateer

Reputation: 17946

Your code is pretty close, but you would have to initialize the style from a fake QLineEdit. The following is based on QLineEdit::paintEvent and QLineEdit::initStyleOption.

#include <QtGui>

class FakeLineEditWidget : public QWidget {
public:
  explicit FakeLineEditWidget(QWidget *parent = NULL) : QWidget(parent) {}
protected:
  void paintEvent(QPaintEvent *) {
    QPainter painter(this);

    QLineEdit dummy;

    QStyleOptionFrameV2 panel;
    panel.initFrom(&dummy);
    panel.rect = QRect(10, 10, 100, 30);  // QFontMetric could provide height.
    panel.lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,
                                           &panel,
                                           &dummy);
    panel.midLineWidth = 0;
    panel.state |= QStyle::State_Sunken;
    panel.features = QStyleOptionFrameV2::None;

    style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &painter, this);
  }
};

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  FakeLineEditWidget w;
  w.setFixedSize(300, 100);
  w.show();

  return app.exec();
}

Upvotes: 2

Related Questions