MainWindow GUI not refreshing/animating, buttons work ok. Pyside 6
-
Hello!
I have built my UI using Qt Designer, and when I use QUiLoader to load it, it works fine, but after I tried to switch to class to overload window closing event, the UI became somewhat non responsive, QEditLine does not show string set with setText() and push buttons are not animated (although I can click them and show file pickers or run configuration dialog, which works fine).
Originally I was using this code to run UI (and it worked fine, but without closing event) :
def showGUI(): app = QApplication(sys.argv) ui_file_name = "interface\\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) cont = Controller(window) window.btnFile.clicked.connect(cont.selectFile) window.btnDirectory.clicked.connect(cont.selectDirectory) window.btnReportDir.clicked.connect(cont.selectReportDirectory) window.ckbPreview.stateChanged.connect(cont.showPreview) window.btnRecognize.clicked.connect(cont.recognize) window.btnDialog.clicked.connect(cont.showDialog) ui_file.close() if not window: print(loader.errorString()) sys.exit(-1) window.show() sys.exit(app.exec_())But I have redesigned it to work like this:
def showGUI2(): app = QApplication(sys.argv) window = MainWindow() window.show() app.processEvents() sys.exit(app.exec_()) class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) ui_file_name = "interface\\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() if (parent): self.ui = loader.load(ui_file, parent) else: self.ui = loader.load(ui_file) ui_file.close() self.cont = Controller(self.ui) self.ui.btnFile.clicked.connect(self.cont.selectFile) self.ui.btnDirectory.clicked.connect(self.cont.selectDirectory) self.ui.btnReportDir.clicked.connect(self.cont.selectReportDirectory) self.ui.ckbPreview.stateChanged.connect(self.cont.showPreview) self.ui.btnRecognize.clicked.connect(self.cont.recognize) self.ui.btnDialog.clicked.connect(self.cont.showDialog) self.ui.installEventFilter(self) def eventFilter(self, watched, event) -> bool: if event.type() == PySide6.QtCore.QEvent.Close: self.closeEvent(event) return True def show(self) -> None: self.ui.show() def closeEvent(self, event): reply = QMessageBox.question(self, 'Confirm quit', "Do you really want to quit?", QMessageBox.Yes, QMessageBox.No) if reply == QMessageBox.Yes: config.Configuration.getInstance().storeConfiguration() event.accept() else: event.ignore()Any ideas as to what I did wrong with initialization of my UI in the class? I normally do Java, so Python is not my stron suit.
I am using pyside 6.0.1 and python 3.7 -
Hi and welcome to devnet,
You don't show your self.ui widget.
Since you decided to use it in a QMainWindow, you should set it as central widget.
-
Thank you.