在Qt设计器和PyQt程序中,垂直布局的大小是不同的

2024-04-24 00:51:32 发布

您现在位置:Python中文网/ 问答频道 /正文

在Qt Designer 5.9中,test.ui中的verticalLayout与窗口边缘有一定距离,但是在使用main.py中的PyQt 5.11.3加载verticalLayout后,verticalLayout会延伸到窗口的边缘。在

主.py

    #!/usr/bin/python3
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        loadUi("test.ui", self)

def main():
    app = QApplication(sys.argv)
    main_window = MainWindow(app)
    main_window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
main()

测试.ui

^{pr2}$

Qt Designer 5.9中test.ui的屏幕截图:

enter image description here

main.py加载test.ui截图:

enter image description here

这种行为的原因是什么?在


Tags: frompytestimportappuimainsys
1条回答
网友
1楼 · 发布于 2024-04-24 00:51:32

如果检查^{cd1>}的代码,您将发现以下代码:

<强>uiparser.py

class UIParser(object):  
    # ...
    def createLayout(self, elem):
        # ...
        margin = -1 if self.stack.topIsLayout() else self.defaults['margin']
        margin = self.wprops.getProperty(elem, 'margin', margin)
        left = self.wprops.getProperty(elem, 'leftMargin', margin)
        top = self.wprops.getProperty(elem, 'topMargin', margin)
        right = self.wprops.getProperty(elem, 'rightMargin', margin)
        bottom = self.wprops.getProperty(elem, 'bottomMargin', margin)
        # A layout widget should, by default, have no margins.
        if self.stack.topIsLayoutWidget():
            if left < 0: left = 0
            if top < 0: top = 0
            if right < 0: right = 0
            if bottom < 0: bottom = 0

    def topIsLayoutWidget(self):
        # A plain QWidget is a layout widget unless it's parent is a
        # QMainWindow or a container widget.  Note that the corresponding uic
        # test is a little more complicated as it involves features not
        # supported by pyuic.

        if type(self[-1]) is not QtWidgets.QWidget:
            return False

        if len(self) < 2:
            return False

        parent = self[-2]

        return isinstance(parent, QtWidgets.QWidget) and type(parent) not in (
                QtWidgets.QMainWindow,
                QtWidgets.QStackedWidget,
                QtWidgets.QToolBox,
                QtWidgets.QTabWidget,
                QtWidgets.QScrollArea,
                QtWidgets.QMdiArea,
                QtWidgets.QWizard,
                QtWidgets.QDockWidget)

问题是由^{cd2>}函数引起的,因为父函数将引用用作基的小部件,在这种情况下,main窗口符合^{{cd3>}的要求,因此^{{cd2>}将返回True,因此,左、上、右,底部为-1,因为这些属性不存在将更新为0,因此将通过消除默认值(9、9、9、9)来建立应用于^{cd5>}的应用程序,但对于Qt设计器,没有更新^{cd5>}以保持其默认值。

因此,最后是一个pyqt bug,它也指出了以下评论:

^{pr2}$

因此,有几种解决方案:

  • 删除:

    if self.stack.topIsLayoutWidget():
        if left < 0: left = 0
        if top < 0: top = 0
        if right < 0: right = 0
        if bottom < 0: bottom = 0
    
  • 使用^{cd7>}:

    #!/usr/bin/python3
    import sys
    from PyQt5 import QtCore, QtWidgets, uic
    
    Ui_Interface, _ = uic.loadUiType('test.ui')
    
    class MainWindow(QtWidgets.QMainWindow, Ui_Interface):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setupUi(self)
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        main_window = MainWindow()
        main_window.show()
        sys.exit(app.exec_())
    
    if __name__ == "__main__":
        main()
    

我更喜欢第二个解决方案,因为源代码不应该修改。

相关问题 更多 >