Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PySide2 QUiLoader returns an empty window

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
like image 314
u2gilles Avatar asked Jan 02 '23 11:01

u2gilles


2 Answers

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_())
like image 107
Ledo Giuliano Vendruscolo Avatar answered Jan 13 '23 02:01

Ledo Giuliano Vendruscolo


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_())
like image 33
eyllanesc Avatar answered Jan 13 '23 02:01

eyllanesc