Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. two different main code loaded the same .ui file and the one is working and other not, why?

two different main code loaded the same .ui file and the one is working and other not, why?

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 4 Posters 528 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Breeze
    wrote on last edited by
    #1

    this is gonna drive me crazy.

    working code is here

    import sys
    from PySide6 import QtCore, QtGui, QtWidgets
    from PySide6.QtUiTools import QUiLoader
    
    loader = QUiLoader()
    app = QtWidgets.QApplication(sys.argv)
    window = loader.load("mainwindow.ui", None)
    window.show()
    sys.exit(app.exec())
    

    not working is here, and this code form official webside link text

    import sys
    from PySide6.QtUiTools import QUiLoader
    from PySide6.QtWidgets import QApplication
    from PySide6.QtCore import QFile, QIODevice
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        ui_file_name = "mainwindow.ui"
        ui_file = QFile(ui_file_name)
        if not ui_file.open(QIODevice.ReadOnly):
            print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
            sys.exit(-1)
        loader = QUiLoader()
        window = loader.load(ui_file)
        ui_file.close()
        if not window:
            print(loader.errorString())
            sys.exit(-1)
        window.show()
    
        sys.exit(app.exec())
    
    

    here is mianwindow.ui code

    <?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>959</width>
        <height>731</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QHBoxLayout" name="horizontalLayout">
        <item>
         <widget class="QWidget" name="widgetLeft" native="true">
          <property name="minimumSize">
           <size>
            <width>350</width>
            <height>0</height>
           </size>
          </property>
          <layout class="QVBoxLayout" name="verticalLayout">
           <item>
            <widget class="QWidget" name="widget" native="true">
             <layout class="QFormLayout" name="formLayout">
              <item row="0" column="0">
               <widget class="QLabel" name="label">
                <property name="text">
                 <string>温室大棚类型</string>
                </property>
               </widget>
              </item>
              <item row="0" column="1">
               <widget class="QComboBox" name="comboBox"/>
              </item>
              <item row="1" column="0">
               <widget class="QLabel" name="label_2">
                <property name="text">
                 <string>风向(度)</string>
                </property>
               </widget>
              </item>
              <item row="1" column="1">
               <widget class="QComboBox" name="comboBox_2"/>
              </item>
             </layout>
            </widget>
           </item>
           <item>
            <widget class="QWidget" name="widget_2" native="true">
             <layout class="QVBoxLayout" name="verticalLayout_2">
              <item>
               <widget class="QGroupBox" name="groupBox">
                <property name="title">
                 <string>尺寸</string>
                </property>
                <layout class="QFormLayout" name="formLayout_2">
                 <item row="0" column="0">
                  <widget class="QLabel" name="label_3">
                   <property name="text">
                    <string>跨度(mm)</string>
                   </property>
                  </widget>
                 </item>
                 <item row="0" column="1">
                  <widget class="QComboBox" name="comboBox_3"/>
                 </item>
                 <item row="1" column="0">
                  <widget class="QLabel" name="label_4">
                   <property name="text">
                    <string>脊高(mm)</string>
                   </property>
                  </widget>
                 </item>
                 <item row="1" column="1">
                  <widget class="QComboBox" name="comboBox_4"/>
                 </item>
                 <item row="2" column="0">
                  <widget class="QLabel" name="label_5">
                   <property name="text">
                    <string>后屋面投影(mm)</string>
                   </property>
                  </widget>
                 </item>
                 <item row="2" column="1">
                  <widget class="QComboBox" name="comboBox_5"/>
                 </item>
                 <item row="3" column="0">
                  <widget class="QLabel" name="label_6">
                   <property name="text">
                    <string>北墙倾角(度)</string>
                   </property>
                  </widget>
                 </item>
                 <item row="3" column="1">
                  <widget class="QComboBox" name="comboBox_6"/>
                 </item>
                 <item row="4" column="0">
                  <widget class="QLabel" name="label_7">
                   <property name="text">
                    <string>北墙高(mm)</string>
                   </property>
                  </widget>
                 </item>
                 <item row="4" column="1">
                  <widget class="QComboBox" name="comboBox_7"/>
                 </item>
                </layout>
               </widget>
              </item>
             </layout>
            </widget>
           </item>
           <item>
            <spacer name="verticalSpacer">
             <property name="orientation">
              <enum>Qt::Vertical</enum>
             </property>
             <property name="sizeHint" stdset="0">
              <size>
               <width>20</width>
               <height>158</height>
              </size>
             </property>
            </spacer>
           </item>
           <item>
            <widget class="QWidget" name="widget_3" native="true">
             <layout class="QHBoxLayout" name="horizontalLayout_2">
              <item>
               <widget class="QPushButton" name="pushButton_3">
                <property name="minimumSize">
                 <size>
                  <width>50</width>
                  <height>0</height>
                 </size>
                </property>
                <property name="maximumSize">
                 <size>
                  <width>50</width>
                  <height>16777215</height>
                 </size>
                </property>
                <property name="text">
                 <string>查询</string>
                </property>
               </widget>
              </item>
              <item>
               <widget class="QPushButton" name="pushButton_2">
                <property name="minimumSize">
                 <size>
                  <width>50</width>
                  <height>0</height>
                 </size>
                </property>
                <property name="maximumSize">
                 <size>
                  <width>50</width>
                  <height>16777215</height>
                 </size>
                </property>
                <property name="text">
                 <string>输出</string>
                </property>
               </widget>
              </item>
              <item>
               <widget class="QPushButton" name="pushButton">
                <property name="minimumSize">
                 <size>
                  <width>50</width>
                  <height>0</height>
                 </size>
                </property>
                <property name="maximumSize">
                 <size>
                  <width>50</width>
                  <height>16777215</height>
                 </size>
                </property>
                <property name="text">
                 <string>退出</string>
                </property>
               </widget>
              </item>
             </layout>
            </widget>
           </item>
          </layout>
         </widget>
        </item>
        <item>
         <widget class="QWidget" name="widgetRight" native="true">
          <layout class="QVBoxLayout" name="verticalLayout_3">
           <item>
            <widget class="QWidget" name="widget_4" native="true">
             <property name="minimumSize">
              <size>
               <width>400</width>
               <height>400</height>
              </size>
             </property>
             <layout class="QHBoxLayout" name="horizontalLayout_3">
              <item>
               <widget class="QWidget" name="widget_5" native="true">
                <property name="minimumSize">
                 <size>
                  <width>400</width>
                  <height>400</height>
                 </size>
                </property>
               </widget>
              </item>
              <item>
               <spacer name="horizontalSpacer">
                <property name="orientation">
                 <enum>Qt::Horizontal</enum>
                </property>
                <property name="sizeHint" stdset="0">
                 <size>
                  <width>0</width>
                  <height>20</height>
                 </size>
                </property>
               </spacer>
              </item>
             </layout>
            </widget>
           </item>
           <item>
            <widget class="QColumnView" name="columnView"/>
           </item>
          </layout>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>959</width>
         <height>22</height>
        </rect>
       </property>
       <widget class="QMenu" name="menu">
        <property name="title">
         <string>文件</string>
        </property>
        <addaction name="actionasd"/>
        <addaction name="action123"/>
       </widget>
       <addaction name="menu"/>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
      <action name="actionasd">
       <property name="text">
        <string>打开图片</string>
       </property>
      </action>
      <action name="action123">
       <property name="text">
        <string>退出</string>
       </property>
      </action>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Which version of PySide6 ?
      On which platform ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Daniel Breeze

        this is gonna drive me crazy.

        working code is here

        import sys
        from PySide6 import QtCore, QtGui, QtWidgets
        from PySide6.QtUiTools import QUiLoader
        
        loader = QUiLoader()
        app = QtWidgets.QApplication(sys.argv)
        window = loader.load("mainwindow.ui", None)
        window.show()
        sys.exit(app.exec())
        

        not working is here, and this code form official webside link text

        import sys
        from PySide6.QtUiTools import QUiLoader
        from PySide6.QtWidgets import QApplication
        from PySide6.QtCore import QFile, QIODevice
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
        
            ui_file_name = "mainwindow.ui"
            ui_file = QFile(ui_file_name)
            if not ui_file.open(QIODevice.ReadOnly):
                print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
                sys.exit(-1)
            loader = QUiLoader()
            window = loader.load(ui_file)
            ui_file.close()
            if not window:
                print(loader.errorString())
                sys.exit(-1)
            window.show()
        
            sys.exit(app.exec())
        
        

        here is mianwindow.ui code

        <?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>959</width>
            <height>731</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <layout class="QHBoxLayout" name="horizontalLayout">
            <item>
             <widget class="QWidget" name="widgetLeft" native="true">
              <property name="minimumSize">
               <size>
                <width>350</width>
                <height>0</height>
               </size>
              </property>
              <layout class="QVBoxLayout" name="verticalLayout">
               <item>
                <widget class="QWidget" name="widget" native="true">
                 <layout class="QFormLayout" name="formLayout">
                  <item row="0" column="0">
                   <widget class="QLabel" name="label">
                    <property name="text">
                     <string>温室大棚类型</string>
                    </property>
                   </widget>
                  </item>
                  <item row="0" column="1">
                   <widget class="QComboBox" name="comboBox"/>
                  </item>
                  <item row="1" column="0">
                   <widget class="QLabel" name="label_2">
                    <property name="text">
                     <string>风向(度)</string>
                    </property>
                   </widget>
                  </item>
                  <item row="1" column="1">
                   <widget class="QComboBox" name="comboBox_2"/>
                  </item>
                 </layout>
                </widget>
               </item>
               <item>
                <widget class="QWidget" name="widget_2" native="true">
                 <layout class="QVBoxLayout" name="verticalLayout_2">
                  <item>
                   <widget class="QGroupBox" name="groupBox">
                    <property name="title">
                     <string>尺寸</string>
                    </property>
                    <layout class="QFormLayout" name="formLayout_2">
                     <item row="0" column="0">
                      <widget class="QLabel" name="label_3">
                       <property name="text">
                        <string>跨度(mm)</string>
                       </property>
                      </widget>
                     </item>
                     <item row="0" column="1">
                      <widget class="QComboBox" name="comboBox_3"/>
                     </item>
                     <item row="1" column="0">
                      <widget class="QLabel" name="label_4">
                       <property name="text">
                        <string>脊高(mm)</string>
                       </property>
                      </widget>
                     </item>
                     <item row="1" column="1">
                      <widget class="QComboBox" name="comboBox_4"/>
                     </item>
                     <item row="2" column="0">
                      <widget class="QLabel" name="label_5">
                       <property name="text">
                        <string>后屋面投影(mm)</string>
                       </property>
                      </widget>
                     </item>
                     <item row="2" column="1">
                      <widget class="QComboBox" name="comboBox_5"/>
                     </item>
                     <item row="3" column="0">
                      <widget class="QLabel" name="label_6">
                       <property name="text">
                        <string>北墙倾角(度)</string>
                       </property>
                      </widget>
                     </item>
                     <item row="3" column="1">
                      <widget class="QComboBox" name="comboBox_6"/>
                     </item>
                     <item row="4" column="0">
                      <widget class="QLabel" name="label_7">
                       <property name="text">
                        <string>北墙高(mm)</string>
                       </property>
                      </widget>
                     </item>
                     <item row="4" column="1">
                      <widget class="QComboBox" name="comboBox_7"/>
                     </item>
                    </layout>
                   </widget>
                  </item>
                 </layout>
                </widget>
               </item>
               <item>
                <spacer name="verticalSpacer">
                 <property name="orientation">
                  <enum>Qt::Vertical</enum>
                 </property>
                 <property name="sizeHint" stdset="0">
                  <size>
                   <width>20</width>
                   <height>158</height>
                  </size>
                 </property>
                </spacer>
               </item>
               <item>
                <widget class="QWidget" name="widget_3" native="true">
                 <layout class="QHBoxLayout" name="horizontalLayout_2">
                  <item>
                   <widget class="QPushButton" name="pushButton_3">
                    <property name="minimumSize">
                     <size>
                      <width>50</width>
                      <height>0</height>
                     </size>
                    </property>
                    <property name="maximumSize">
                     <size>
                      <width>50</width>
                      <height>16777215</height>
                     </size>
                    </property>
                    <property name="text">
                     <string>查询</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QPushButton" name="pushButton_2">
                    <property name="minimumSize">
                     <size>
                      <width>50</width>
                      <height>0</height>
                     </size>
                    </property>
                    <property name="maximumSize">
                     <size>
                      <width>50</width>
                      <height>16777215</height>
                     </size>
                    </property>
                    <property name="text">
                     <string>输出</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <widget class="QPushButton" name="pushButton">
                    <property name="minimumSize">
                     <size>
                      <width>50</width>
                      <height>0</height>
                     </size>
                    </property>
                    <property name="maximumSize">
                     <size>
                      <width>50</width>
                      <height>16777215</height>
                     </size>
                    </property>
                    <property name="text">
                     <string>退出</string>
                    </property>
                   </widget>
                  </item>
                 </layout>
                </widget>
               </item>
              </layout>
             </widget>
            </item>
            <item>
             <widget class="QWidget" name="widgetRight" native="true">
              <layout class="QVBoxLayout" name="verticalLayout_3">
               <item>
                <widget class="QWidget" name="widget_4" native="true">
                 <property name="minimumSize">
                  <size>
                   <width>400</width>
                   <height>400</height>
                  </size>
                 </property>
                 <layout class="QHBoxLayout" name="horizontalLayout_3">
                  <item>
                   <widget class="QWidget" name="widget_5" native="true">
                    <property name="minimumSize">
                     <size>
                      <width>400</width>
                      <height>400</height>
                     </size>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <spacer name="horizontalSpacer">
                    <property name="orientation">
                     <enum>Qt::Horizontal</enum>
                    </property>
                    <property name="sizeHint" stdset="0">
                     <size>
                      <width>0</width>
                      <height>20</height>
                     </size>
                    </property>
                   </spacer>
                  </item>
                 </layout>
                </widget>
               </item>
               <item>
                <widget class="QColumnView" name="columnView"/>
               </item>
              </layout>
             </widget>
            </item>
           </layout>
          </widget>
          <widget class="QMenuBar" name="menubar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>959</width>
             <height>22</height>
            </rect>
           </property>
           <widget class="QMenu" name="menu">
            <property name="title">
             <string>文件</string>
            </property>
            <addaction name="actionasd"/>
            <addaction name="action123"/>
           </widget>
           <addaction name="menu"/>
          </widget>
          <widget class="QStatusBar" name="statusbar"/>
          <action name="actionasd">
           <property name="text">
            <string>打开图片</string>
           </property>
          </action>
          <action name="action123">
           <property name="text">
            <string>退出</string>
           </property>
          </action>
         </widget>
         <resources/>
         <connections/>
        </ui>
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Daniel-Breeze said in two different main code loaded the same .ui file and the one is working and other not, why?:

        not working is here,

        In what way "not working"? Did you step through it to make sure where it got to?

        D 1 Reply Last reply
        0
        • JonBJ JonB

          @Daniel-Breeze said in two different main code loaded the same .ui file and the one is working and other not, why?:

          not working is here,

          In what way "not working"? Did you step through it to make sure where it got to?

          D Offline
          D Offline
          Daniel Breeze
          wrote on last edited by
          #4

          @JonB

          I change that like this

          import sys
          
          from PySide6.QtWidgets import QApplication
          from PySide6.QtUiTools import QUiLoader
          
          
          if __name__ == "__main__":
              app = QApplication(sys.argv)
          
          #    ui_file_name = "mainwindow.ui"
          #    ui_file = QFile(ui_file_name)
          #    if not ui_file.open(QIODevice.ReadOnly):
          #        print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
          #        sys.exit(-1)
              print("hah")
              loader = QUiLoader()
              print("hah")
              window = loader.load("mainwindow.ui")
          #    ui_file.close()
          #    if not window:
          #        print(loader.errorString())
          #        sys.exit(-1)
              window.show()
          
              sys.exit(app.exec())
          

          and I run the program with debugging, when it execute loader = QUiLoader() it stoped, and it can't execute next line. like there is one "hah" be printed

          JonBJ 1 Reply Last reply
          0
          • D Daniel Breeze

            @JonB

            I change that like this

            import sys
            
            from PySide6.QtWidgets import QApplication
            from PySide6.QtUiTools import QUiLoader
            
            
            if __name__ == "__main__":
                app = QApplication(sys.argv)
            
            #    ui_file_name = "mainwindow.ui"
            #    ui_file = QFile(ui_file_name)
            #    if not ui_file.open(QIODevice.ReadOnly):
            #        print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
            #        sys.exit(-1)
                print("hah")
                loader = QUiLoader()
                print("hah")
                window = loader.load("mainwindow.ui")
            #    ui_file.close()
            #    if not window:
            #        print(loader.errorString())
            #        sys.exit(-1)
                window.show()
            
                sys.exit(app.exec())
            

            and I run the program with debugging, when it execute loader = QUiLoader() it stoped, and it can't execute next line. like there is one "hah" be printed

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Daniel-Breeze
            Although it seems the wrong way round to me, in your "working" first code case you have the loader = QUiLoader() statement before you create the QApplication() object. Try moving your creation of QUiLoader() to come before QApplication() in the second non-working case?

            D 1 Reply Last reply
            1
            • JonBJ JonB

              @Daniel-Breeze
              Although it seems the wrong way round to me, in your "working" first code case you have the loader = QUiLoader() statement before you create the QApplication() object. Try moving your creation of QUiLoader() to come before QApplication() in the second non-working case?

              D Offline
              D Offline
              Daniel Breeze
              wrote on last edited by
              #6

              @JonB
              thanks a lot, solved. but if you need create loader first, why that official example didn't, I'am really confused.

              JonBJ 1 Reply Last reply
              0
              • D Daniel Breeze has marked this topic as solved on
              • D Daniel Breeze

                @JonB
                thanks a lot, solved. but if you need create loader first, why that official example didn't, I'am really confused.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Daniel-Breeze
                I'm actually surprised that works. You would think the QApplication ought to come first, but it was the most obvious difference I could spot.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  friedemannkleint
                  wrote on last edited by
                  #8

                  If this is 6.6; it might be a manifestation of https://bugreports.qt.io/browse/QTBUG-118317 .

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved