How use .ui files in PyQT?
-
Hi!
How use .ui files in PyQT?
I find this, but here no file ui_mainwindow and code not work.class MainWindow(QMainWindow): def __init__(self): # super(MainWindow, self).__init__() # self.ui = Ui_MainWindow() # self.ui.setupUi(self) ui_file = QFile("./mainwindow.ui") ui_file.open(QFile.ReadOnly) loader = QUiLoader() window = loader.load(ui_file) window.show() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() # window.show() sys.exit(app.exec_())
-
sory, its work:
import sys from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication from PySide2.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("Cannot open {}: {}".format(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_())
-
@Mikeeeeee
You are dynamically loading yourui
file at run-time. That is fine, but more code for you to write, no code completion at design-time, no class/member variables etc. for the form, you must supply theui
file at run-time, and so on.Are you aware that you can --- if you wish --- use the
pyuic5
program supplied with PyQt5 to produce Python/PyQt5 code for yourui
files instead of loading them dynamically at runtime, with corresponding advantages? An explanation is at https://stackoverflow.com/questions/52471705/why-in-pyqt5-should-i-use-pyuic5-and-not-uic-loaduimy-ui, with clickable link for pyuic5 to get you going. -
use pyuic5 -x interface.ui and then inspect the __main__ created to test the interface. That should enlighten you as to how to instantiate it, if you understand python.
-
It is not work
class CdViewer(QObject): def __init__(self): super(CdViewer, self).__init__() uic.loadUi("cdViewer.ui", self) self.show()
It is work, but how I can work with widgets and get signals?
if __name__ == '__main__': app = Qt.QApplication([]) win = uic.loadUi("cdViewer.ui") win.show() app.exec()
-
It is works:
from PyQt5 import QtCore, QtGui, QtWidgets, uic from PyQt5 import Qt # from PyQt5 import QtNetwork from PySide2.QtCore import QObject # from PyQt5.QtCore import QCoreApplication, QUrl import sys class CdViewer(QObject): # ui = uic.loadUi("cdViewer.ui") def __init__(self): # super(CdViewer, self).__init__() self.ui = uic.loadUi("cdViewer.ui") self.ui.label_13.setText("asdf") def showWindow(self): self.ui.show() if __name__ == '__main__': app = Qt.QApplication([]) # win = uic.loadUi("cdViewer.ui") # win.show() # sys.exit(app.exec()) cdViewer = CdViewer() cdViewer.showWindow() # cdViewer.show() app.exec()