How does designer create a Line widget?

daisy picture daisy · Apr 7, 2012 · Viewed 24.5k times · Source

In Qt Designer , you can drag a "Line" widget , which will create a line in your layout.

But I checked the document and headers , I didn't find the "Line" header / widget , what was it ?

Answer

kaveish picture kaveish · Feb 9, 2017

In Qt 5.7 the code generated by Qt Designer for a Horizontal Line (which can be checked in the menu using "Form/View Code...") is:

QFrame *line;
line = new QFrame(Form);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);

This will create the lines you see in Qt Designer.

The current answers do not seem to give working solutions, here is a comparison of all answers (this solution is the first line):

Horizontal lines in Qt

Full code:

#include <QtWidgets>

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

  QWidget widget;
  auto layout = new QVBoxLayout;
  widget.setLayout(layout);
  widget.resize(200, 200);

  auto lineA = new QFrame;
  lineA->setFrameShape(QFrame::HLine);
  lineA->setFrameShadow(QFrame::Sunken);
  layout->addWidget(lineA);

  QWidget *lineB = new QWidget;
  lineB->setFixedHeight(2);
  lineB->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  lineB->setStyleSheet(QString("background-color: #c0c0c0;"));
  layout->addWidget(lineB);

  auto lineC = new QFrame;
  lineC->setFixedHeight(3);
  lineC->setFrameShadow(QFrame::Sunken);
  lineC->setLineWidth(1);
  layout->addWidget(lineC);

  QFrame* lineD = new QFrame;
  lineD->setFrameShape(QFrame::HLine);
  layout->addWidget(lineD);

  widget.show();
  return app.exec();
}