如何在 Qt 中以编程方式制作一条水平线

发布于 2024-11-01 06:26:08 字数 529 浏览 1 评论 0原文

我想弄清楚如何在 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔女人霸气范 2024-11-08 06:26:08

水平线或垂直线只是一个设置了一些属性的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);

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);
邮友 2024-11-08 06:26:08

这是使用 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 示例

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:

Example of QHLine on Windows 10

这是一个使用标准 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())

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())
别想她 2024-11-08 06:26:08

你可以用这个

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文