How to programmatically make a horizontal line in Qt
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);
Here's another solution using PySide:
from PySide.QtGui import QFrame
class QHLine(QFrame):
def __init__(self):
super(QHLine, self).__init__()
self.setFrameShape(QFrame.HLine)
self.setFrameShadow(QFrame.Sunken)
class QVLine(QFrame):
def __init__(self):
super(QVLine, self).__init__()
self.setFrameShape(QFrame.VLine)
self.setFrameShadow(QFrame.Sunken)
Which can then be used as (for example):
from PySide.QtGui import QApplication, QWidget, QGridLayout, QLabel, QComboBox
if __name__ == "__main__":
app = QApplication([])
widget = QWidget()
layout = QGridLayout()
layout.addWidget(QLabel("Test 1"), 0, 0, 1, 1)
layout.addWidget(QComboBox(), 0, 1, 1, 1)
layout.addWidget(QHLine(), 1, 0, 1, 2)
layout.addWidget(QLabel("Test 2"), 2, 0, 1, 1)
layout.addWidget(QComboBox(), 2, 1, 1, 1)
widget.setLayout(layout)
widget.show()
app.exec_()
Which results in the following: