Search code examples
pythonpyqtpyqt5qt-designer

The UI designed by Qtdesigner is not the same as the one previewed in Qtdesigner when translated into Python code


I guess the problem is layout, and I want to make the top line with the buttons in the Frame, Horizontal Layout;Below is the TabWidget with a table in it. The TabWidget and Frame use Vertical Layout.Previews in Qtdesigner are normal, but a BUG appears after the py file is converted. I made a smaller example to illustrate the problem. Here is the UI file code:

<?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>

I'm going to post some pictures, but I'm not sure I can, I've never uploaded pictures here...Orz. Here is a preview from Qtdesigner: designer preview

Here is the result that runs in the pycharm:

running screenshot

Here is the diagram:

inspector preview


Solution

  • change class MyApp(QtWidgets.QMainWindow, Ui_MainWindow): to 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_())
    

    enter image description here