【问题标题】:The UI designed by Qtdesigner is not the same as the one previewed in Qtdesigner when translated into Python code [duplicate]Qtdesigner设计的UI与翻译成Python代码时在Qtdesigner中预览的UI不同[重复]
【发布时间】:2019-12-24 01:55:12
【问题描述】:

我猜问题是布局,我想用Frame中的按钮制作顶行,Horizo​​ntal Layout;下面是TabWidget,里面有一个表格。 TabWidget和Frame使用Vertical Layout。Qtdesigner中的Previews正常,但是py文件转换后出现BUG。 我做了一个小例子来说明这个问题。这是UI文件代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1191</width>
    <height>941</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_3">
   <item>
    <layout class="QVBoxLayout" name="verticalLayout_2">
     <item>
      <widget class="QFrame" name="frame">
       <property name="frameShape">
        <enum>QFrame::StyledPanel</enum>
       </property>
       <property name="frameShadow">
        <enum>QFrame::Raised</enum>
       </property>
       <layout class="QHBoxLayout" name="horizontalLayout">
        <item>
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QPushButton" name="pushButton_2">
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
        </item>
        <item>
         <spacer name="horizontalSpacer">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>940</width>
            <height>20</height>
           </size>
          </property>
         </spacer>
        </item>
       </layout>
      </widget>
     </item>
     <item>
      <widget class="QTabWidget" name="tabWidget">
       <property name="tabPosition">
        <enum>QTabWidget::West</enum>
       </property>
       <property name="currentIndex">
        <number>0</number>
       </property>
       <widget class="QWidget" name="tab">
        <attribute name="title">
         <string>Tab 1</string>
        </attribute>
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QTableWidget" name="tableWidget">
           <property name="rowCount">
            <number>5</number>
           </property>
           <row/>
           <row/>
           <row/>
           <row/>
           <row/>
           <column>
            <property name="text">
             <string>A</string>
            </property>
           </column>
           <column>
            <property name="text">
             <string>B</string>
            </property>
           </column>
           <column>
            <property name="text">
             <string>C</string>
            </property>
           </column>
           <column>
            <property name="text">
             <string>D</string>
            </property>
           </column>
          </widget>
         </item>
        </layout>
       </widget>
       <widget class="QWidget" name="tab_2">
        <attribute name="title">
         <string>Tab 2</string>
        </attribute>
       </widget>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

我要发布一些图片,但我不确定我能不能,我从来没有在这里上传过图片...Orz。 这是来自 Qtdesigner 的预览:

这是在pycharm中运行的结果:

这是图表:

【问题讨论】:

  • 从这里看起来很好。您确定您使用的是正确的 ui 文件,如果您使用的是 pyuic,您是否在保存后重新构建了它?
  • 通过S Nick的回答,问题解决了。感谢您百忙之中抽空帮助我
  • 如果 SNick 给出的答案对您有用,请不要忘记将其标记为已接受!顺便说一句,请记住始终在您的回答中提供minimal, reproducible examples:您很幸运 SNick 确实考虑了错误的类继承可能性,但您不能总是期望其他人会进行那种程度的“猜测” ... :-)
  • 谢谢指教,我刚开始用Stack Overflow,所以比较麻烦,以后会注意的

标签: python pyqt pyqt5 qt-designer


【解决方案1】:

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow): 更改为class MyApp(QtWidgets.QWidget, Ui_MainWindow):

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

qtCreatorFile = "test22.ui"   
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

#class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
class MyApp(QtWidgets.QWidget, Ui_MainWindow):
    def __init__(self):
        super().__init__()

        Ui_MainWindow.__init__(self)
        self.setupUi(self)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

【讨论】:

  • 非常感谢!问题已经解决了!
猜你喜欢
  • 2017-04-26
  • 2020-08-29
  • 1970-01-01
  • 2011-10-31
  • 2011-01-24
  • 2021-07-06
  • 2020-11-06
  • 2019-02-01
  • 2022-12-07
相关资源
最近更新 更多