如何在Qt中以编程方式绘制水平线

40 投票
4 回答
59344 浏览
提问于 2025-04-16 15:46

我想知道怎么在Qt中画一条横线。在设计器里做这个很简单,但我想通过编程的方式来实现。我查了一些资料,也看了ui文件里的xml内容,但还是没搞明白。

这是ui文件里的xml内容:

  <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>

4 个回答

9

这里有一个使用标准PyQt5的解决方案,我是根据shoosh的回答得出的:

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

如果你想把它添加到一个网格中,比如说:

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)

要注意,千万不要对分隔符使用对齐,否则可能会出现问题,因为它们可能无法正确缩放。

为了让大家更清楚,这里是如何把它添加到你的窗口中的:

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

这里有一个使用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)

然后可以这样使用(例如):

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_()

这样会得到以下结果:

Windows 10上QHLine的示例

46

一条水平线或垂直线其实就是一个设置了特定属性的 QFrame。在C++中,生成一条线的代码大概是这样的:

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

撰写回答