Warm tip: This article is reproduced from stackoverflow.com, please click
pyqt python QT

How to programmatically make a horizontal line in Qt

发布于 2020-07-16 05:06:52

I'm trying to figure out how to make a horizontal line in Qt. This is easy to create in Designer but I want to create one programmatically. I've done some googleing and looked at the xml in a ui file but haven't been able to figure anything out.

This is what the xml from the ui file looks like:

  <widget class="Line" name="line">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>110</y>
     <width>118</width>
     <height>3</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
Questioner
keegan3d
Viewed
32
153k 2017-11-19 08:06

A horizontal or vertical line is just a QFrame with some properties set. In C++, the code that is generated to create a line looks like this:

line = new QFrame(w);
line->setObjectName(QString::fromUtf8("line"));
line->setGeometry(QRect(320, 150, 118, 3));
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);