【问题标题】:QtDesigner preview different from the actual GUIQtDesigner 预览不同于实际的 GUI
【发布时间】:2017-04-26 22:50:21
【问题描述】:

Python 3.4、PyQt5、QtDesigner

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

QtDesigner 预览和应用程序 GUI:

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>

.ui 文件使用 pyuic 转换为 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_())

【问题讨论】:

  • 请提供minimal reproducible example(强调minimal)。
  • @Mailerdaimon 添加了一个最小示例。将 .py 文件替换为 .ui 文件。
  • @zdub。组合框和主窗口都没有布局,所以很难猜出你期望从这个例子中得到什么行为。当我在 Qt Designer 中查看它时,它看起来不像您的任何一个屏幕截图。如果您不以一致的方式使用布局,则结果将是严格未定义的。
  • 你想要什么?一个正在工作且可调整大小的 UI,或者一个在设计器中看起来完全一样但无需调整大小的 UI。 Qt 中的常规方法是使用布局。布局确保您的 UI 即使在调整大小或更改纵横比时也能正常工作,但小部件可能会更改其大小以使其正常工作。另一种方法是为每个 UI 元素设置固定大小。我不建议这样做!阅读更多关于 Qt 布局系统here.
  • @Mailerdaimon 好吧,如果结果会有所不同,为什么他们首先要创建设计师?它也发生在我身上。预览与新转换的python版本不同

标签: python user-interface pyqt pyqt5 qt-designer


【解决方案1】:

当我最初创建 GUI 时,我首先添加了一个网格布局小部件 QGridLayout,然后用项目填充它,然后将其拖到上面并放置在组框容器 QGroupBox 中。

我完全不使用QGridLayout 解决了这个问题,而是简单地将项目添加到QGroupBox,然后将网格布局应用于容器。应用程序 GUI 现在与 QtDesigner 预览匹配。

【讨论】:

  • 很遗憾。我的 UI 取决于网格。所以这不能是我的答案(我和你有同样的问题)。我已经在 QGroupBox 中添加了零运气的项目
猜你喜欢
  • 2020-11-06
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
相关资源
最近更新 更多