two different main code loaded the same .ui file and the one is working and other not, why?
-
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>
-
Hi and welcome to devnet,
Which version of PySide6 ?
On which platform ? -
@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?
-
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
-
@Daniel-Breeze
Although it seems the wrong way round to me, in your "working" first code case you have theloader = QUiLoader()
statement before you create theQApplication()
object. Try moving your creation ofQUiLoader()
to come beforeQApplication()
in the second non-working case? -
-
@Daniel-Breeze
I'm actually surprised that works. You would think theQApplication
ought to come first, but it was the most obvious difference I could spot. -
If this is 6.6; it might be a manifestation of https://bugreports.qt.io/browse/QTBUG-118317 .