How to programmatically make a horizontal line in Qt
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开发者_JS百科 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>
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:
Here is a solution using standard PyQt5 that I derived from shoosh's answer:
from PyQt5 import QtWidgets
class QHSeparationLine(QtWidgets.QFrame):
'''
a horizontal separation 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)
return
class QVSeparationLine(QtWidgets.QFrame):
'''
a vertical separation 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):
separator_vertical = separation_lines.QVSeparationLine()
separator_horizontal = separation_lines.QHSeparationLine()
grid = QtWidgets.QGridLayout()
grid.addWidget(your_widget_left_from_vertical_separator, 0, 0, 1, 1,)
grid.addWidget(separator_vertical, 0, 1, 1, 1)
grid.addWidget(your_widget_right_from_vertical_separator, 0, 2, 1, 1,)
grid.addWidget(separator_horizontal, 1, 0, 1, 2)
grid.addWidget(your_widget_below_horizontal_spacer, 2, 0, 1, 2)
Make sure to never use alignment on the separators, 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 sys
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = QtWidgets.QWidget()
widget.setLayout(grid)
widget.show()
sys.exit(app.exec())
You can use this
self.line = QFrame()
self.line.setGeometry(QRect(60, 110, 751, 20))
self.line.setFrameShape(QFrame.HLine)
self.line.setFrameShadow(QFrame.Sunken)
self.layout.addWidget(self.line)
精彩评论