How to programmatically make a horizontal line in Qt How to programmatically make a horizontal line in Qt python python

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 QFrameclass 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, QComboBoxif __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:

Example of QHLine on Windows 10


Here is a solution using standard PyQt5 that I derived from shoosh's answer:

from PyQt5 import QtWidgetsclass QHSeperationLine(QtWidgets.QFrame):  '''  a horizontal seperation line\n  '''  def __init__(self):    super().__init__()    self.setMinimumWidth(1)    self.setFixedHeight(20)    self.setFrameShape(QtWidgets.QFrame.HLine)    self.setFrameShadow(QtWidgets.QFrame.Sunken)    self.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)    returnclass QVSeperationLine(QtWidgets.QFrame):  '''  a vertical seperation line\n  '''  def __init__(self):    super().__init__()    self.setFixedWidth(20)    self.setMinimumHeight(1)    self.setFrameShape(QtWidgets.QFrame.VLine)    self.setFrameShadow(QtWidgets.QFrame.Sunken)    self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)    return

And if you want to add it (for example to a grid):

seperator_vertical = seperation_lines.QVSeperationLine()seperator_horizontal = seperation_lines.QHSeperationLine()grid = QtWidgets.QGridLayout()grid.addWidget(your_widget_left_from_vertical_seperator, 0, 0, 1, 1,)grid.addWidget(seperator_vertical, 0, 1, 1, 1)grid.addWidget(your_widget_right_from_vertical_seperator, 0, 2, 1, 1,)grid.addWidget(seperator_horizontal, 1, 0, 1, 2)grid.addWidget(your_widget_below_horizontal_spacer, 2, 0, 1, 2)

Make sure to never use alignment on the seperators, otherwise it will probably screw you over because they will not scale properly.

Just to show everything here is how to add it to your window:

import sysif __name__ == "__main__":    app = QtWidgets.QApplication([])    widget = QtWidgets.QWidget()    widget.setLayout(grid)    widget.show()    sys.exit(app.exec())