6

PySide2(5.6.0~a1) Qt UI file loader returns an empty window whereare PyQt5 loader works fine. Could you explained to me where I am wrong.

Non Working PySide2 version:

import sys
from PySide2.QtWidgets import QDialog, QApplication
from PySide2 import QtUiTools

class AppWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = QtUiTools.QUiLoader().load("dialog1.ui")
        self.show()

app = QApplication(sys.argv)
w = AppWindow()
sys.exit(app.exec_())

Working PyQt5 version:

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5 import uic

class AppWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi("dialog1.ui", self)
        self.show()

app = QApplication(sys.argv)
w = AppWindow()
sys.exit(app.exec_())

Using this function also does not work :

def loadUiWidget(uifilename, parent=None):
    loader = QtUiTools.QUiLoader()
    uifile = QtCore.QFile(uifilename)
    uifile.open(QtCore.QFile.ReadOnly)
    ui = loader.load(uifile, parent)
    uifile.close()
    return ui
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
u2gilles
  • 6,888
  • 7
  • 51
  • 75

3 Answers3

5

QUiLoader().load() returns the widget as an object so if you assign it to a variable it will not do anything, you should use show():

import sys
from PySide2.QtWidgets import QApplication
from PySide2 import QtUiTools

app = QApplication(sys.argv)
w = QtUiTools.QUiLoader().load("dialog1.ui")
w.show()
sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Thanks Eyllanesc. In my case, I made the following change : self.ui.show() – u2gilles May 02 '18 at 09:07
  • 2
    .... It works but I lost the signal/slot configuration I made in Qt Designer, which is not a big deal. Do you know a way to achieve the same result as in PyQt5, that is, all widgets created in Qt Designer are instance variables of class Mydialog? – u2gilles May 02 '18 at 09:53
  • @u2gilles were you able to find an answer to this? I'm facing the same thing... thanks! – wspeirs Feb 27 '19 at 12:58
5

In PySide2 there's no function to QMainWindow class overwrite itself. It's necessary to show the ui:

import sys
from PySide2.QtWidgets import QDialog, QApplication
from PySide2 import QtUiTools

class AppWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = QtUiTools.QUiLoader().load("dialog1.ui")
        self.ui.show()

app = QApplication(sys.argv)
w = AppWindow()
sys.exit(app.exec_())
  • This is just wrong. Besides the fact that it's unclear what that QMainWindow reference should mean, that `AppWindow` class and its instance become almost useless if used like that, since the only window actually shown and used is the `self.ui` member. That's ***not*** how QUiLoader is supposed to be used. – musicamante May 14 '23 at 11:11
4

If you want to load QMainWindow from designer *.ui file you can use

import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setCentralWidget(QUiLoader().load("form.ui"))

but slot-signal bindings, which are set in the designer in *.ui file, are not working anyway.

So, for full-function use of designer GUI and slot-signal bindings, the only way I found is to compile *.ui file to python module with pyside UI compiler:

pyside2-uic mainwindow.ui > ui_mainwindow.py

and then include produced ui_mainwindow. In this method the slot-signal pairs from Qt UI designer will work well.

import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
Ornstein89
  • 598
  • 1
  • 6
  • 15