【问题标题】:How to programmatically make a horizontal line in Qt如何以编程方式在 Qt 中制作水平线
【发布时间】:2011-08-05 23:51:53
【问题描述】:

我试图弄清楚如何在 Qt 中制作一条水平线。这很容易在 Designer 中创建,但我想以编程方式创建一个。我已经做了一些谷歌搜索并查看了 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>

【问题讨论】:

    标签: python qt pyqt


    【解决方案1】:

    水平或垂直线只是带有一些属性的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);
    

    【讨论】:

    • setGeometry 和 setFrameShadow 真的有必要吗?我对 Qt 还是很陌生,但我希望框架阴影的样式默认取决于整体 Qt UI 样式。我正在尝试在这里编写跨平台。
    • @MichaelScheper 没有,唯一需要调用的是setFrameShape() 和setFrameShadow()。
    【解决方案2】:

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

    结果如下:

    【讨论】:

    • 对于未来的读者,PySide 只兼容 Python 3.6 或 3.6 尝试安装 PySide2,4,5,6
    【解决方案3】:

    这是一个使用标准 PyQt5 的解决方案,我从 shoosh 的回答中得出:

    from PyQt5 import QtWidgets
    
    class 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)
        return
    
    class 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
    

    如果你想添加它(例如添加到网格中):

    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)
    

    确保永远不要在分隔符上使用对齐,否则它可能会搞砸你,因为它们无法正确缩放。

    这里只是展示如何将它添加到您的窗口:

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

    【讨论】:

      【解决方案4】:

      你可以用这个

      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)
      

      【讨论】:

      • 你的意思是用c++的那个?
      • 我看到前面的 3 个答案是相同基本答案(QFrame.HLine 和 QFrame.Sunken)的翻译,但用 C++、pyside 和 pyqt 编写。你的回答又带来了什么?
      • 所有这些都是他们创建的类,还有其他人想要使用 python 的简单解决方案,我对 c++ 语言不感兴趣,当我看到关于它的帖子时,我不会阅读或理解它们全部,我没有看到你的请停止这种评论,让人们知道我们不在课堂上
      • IMO,我没有看到任何新内容。对我来说,这是一个简单的副本,没有任何有趣的东西,所以我给了我的 DV。例如,您可以创建一个返回具有这些属性的 QFrame 的函数,但它仍然是一个副本。基础是一样的。
      猜你喜欢
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 2014-05-06
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多