How does designer create a Line widget?
Asked Answered
H

4

22

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 ?

Hick answered 7/4, 2012 at 10:52 Comment(0)
J
28

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();
}
Jahncke answered 9/2, 2017 at 14:48 Comment(1)
And is it possible with your solution to use lineD several times to put this line in different places...? Or should we create a new line each time...?Malinger
A
10

I guess you mean a horizontal / vertical line widget: it's just a simple QWidget with a gray background color and the horizontal is a fix height (1-3 pixel) and expanding width widget, the vertical is a fix width expanding height widget.

Horizontal example code:

QWidget *horizontalLineWidget = new QWidget;
horizontalLineWidget->setFixedHeight(2);
horizontalLineWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
horizontalLineWidget->setStyleSheet(QString("background-color: #c0c0c0;"));
Alcantara answered 6/5, 2012 at 8:5 Comment(0)
O
8

Check out QFrame::setFrameShape(). To get a line, use either QFrame::HLine or QFrame::VLine as the function's argument.

// Create a horizontal line by creating a frame and setting its shape to QFrame::HLine:
QFrame* hFrame = new QFrame;
hFrame->setFrameShape(QFrame::HLine);

// Create a vertical line by creating a frame and setting its shape to QFrame::VLine:
QFrame* vFrame = new QFrame;
vFrame->setFrameShape(QFrame::VLine);
Otiose answered 22/5, 2015 at 17:22 Comment(0)
F
5

It is a QFrame with height 3, sunken shadow and line width equal to 1. You can see it if examine header generated by uic tool.

Frosting answered 18/12, 2012 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.