QtDesigner预览与实际GUI不同

4

Python 3.4, PyQt5, QtDesigner

我使用QtDesigner制作了一个GUI,但是从中生成的python应用程序GUI与QtDesigner显示的预览不匹配。似乎问题与我的网格布局的尺寸/间距有关。我没有手动编辑GUI中的任何内容,所有代码都是由QtDesigner创建的。

QtDesigner预览和应用程序GUI:

enter image description here

QtDesigner .ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QGroupBox" name="groupBox">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>20</y>
      <width>131</width>
      <height>111</height>
     </rect>
    </property>
    <property name="title">
     <string>GroupBox</string>
    </property>
    <widget class="QWidget" name="gridLayoutWidget">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>20</y>
       <width>111</width>
       <height>81</height>
      </rect>
     </property>
     <layout class="QGridLayout" name="gridLayout">
      <property name="spacing">
       <number>5</number>
      </property>
      <item row="1" column="0">
       <widget class="QLabel" name="label">
        <property name="text">
         <string>a =</string>
        </property>
       </widget>
      </item>
      <item row="1" column="3">
       <widget class="QLineEdit" name="lineEdit_3"/>
      </item>
      <item row="2" column="2">
       <widget class="QLineEdit" name="lineEdit_5"/>
      </item>
      <item row="2" column="1">
       <widget class="QLineEdit" name="lineEdit_4"/>
      </item>
      <item row="1" column="1">
       <widget class="QLineEdit" name="lineEdit"/>
      </item>
      <item row="3" column="3">
       <widget class="QLineEdit" name="lineEdit_9"/>
      </item>
      <item row="3" column="1">
       <widget class="QLineEdit" name="lineEdit_7"/>
      </item>
      <item row="2" column="0">
       <widget class="QLabel" name="label_2">
        <property name="text">
         <string>b =</string>
        </property>
       </widget>
      </item>
      <item row="2" column="3">
       <widget class="QLineEdit" name="lineEdit_6"/>
      </item>
      <item row="1" column="2">
       <widget class="QLineEdit" name="lineEdit_2"/>
      </item>
      <item row="3" column="0">
       <widget class="QLabel" name="label_3">
        <property name="text">
         <string>c =</string>
        </property>
       </widget>
      </item>
      <item row="3" column="2">
       <widget class="QLineEdit" name="lineEdit_8"/>
      </item>
      <item row="0" column="1" colspan="3">
       <widget class="QLabel" name="label_4">
        <property name="text">
         <string>(   x  ,    y   ,   z   )</string>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

使用pyuic将.ui文件转换为testGUI.py。

以下是应用程序代码:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from testGUI import Ui_MainWindow

class App(Ui_MainWindow):
    def __init__(self, dialog):
        Ui_MainWindow.__init__(self)
        self.setupUi(dialog)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    dialog = QtWidgets.QMainWindow()

    testGUI = App(dialog)

    dialog.show()
    sys.exit(app.exec_())

请提供一个 [MCVE](重点在于“minimal”)。 - Mailerdaimon
@Mailerdaimon 添加了一个最小化的示例。用 .ui 文件替换了 .py 文件。 - zdub
1
@zdub. 无论是 group-box 还是 main-window 都没有布局,因此很难猜测您在这个示例中期望的行为。当我在 Qt Designer 中查看它时,它看起来与您的任何屏幕截图都不同。如果您没有以一致的方式使用布局,则结果将严格未定义。 - ekhumoro
1
你想要什么?一个可调整大小的工作界面还是一个与设计师完全相同但不可调整大小的界面。在Qt中,通常的方法是使用布局(Layouts)。布局确保您的UI即使在调整大小或更改纵横比时也能正常工作,但为此小部件可能会改变其大小。另一种方法是为每个UI元素设置固定大小。我不建议这样做!在这里了解更多关于Qt布局系统的信息。 - Mailerdaimon
@Mailerdaimon,为什么他们一开始要创建设计师,如果最终结果还是会有所不同呢?这也发生在我身上。预览与新转换的Python版本不同。 - greendino
@lone_coder,因为这不是设计师的重点,它无法为灵活的UI提供精确的预览,因为UI是灵活的。如果一切都是固定的,而设计师的输出仍然不适合,请提交错误报告并询问qt维护者。 - Mailerdaimon
1个回答

2
当我最初创建GUI时,我首先添加了一个网格布局小部件QGridLayout,然后填充它的项目,然后将其拖动并放置在组框容器QGroupBox中。为了解决这个问题,我根本不使用QGridLayout,而是直接将项目添加到QGroupBox,然后将网格布局应用于容器。应用程序GUI现在与QtDesigner预览相匹配。

遗憾的是,我的UI依赖于网格。所以这不能成为我的答案(我和你一样有同样的问题)。我已经尝试了向QGroupBox添加项目,但没有成功。 - greendino

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接