I have a directory/folder containing two files, MainWindow.py Mainwindow.ui
I have designed my MainWindow.ui in QtDesigner6 and converteded the output file to MainWindow.ui
I have a code window that launches a window/form and is displayed in it's full glory a blank window that is shown and can be closed.
from PySide6.QtWidgets import QApplication, QWidget
import sys
app = QApplication(sys.argv)
window = QWidget()
window.show()
app.exec_()
This code I have grabbed from a book, my question is how to get this code to launch my converted MainWindow.ui file instead of just a blank window?
I have tried changing the line to:window = MainWindow() thus deleting the QWidget and other permitations but not showing my nicly crafted ui
The form file is not used in your application automatically, you have to manually load the
file and create the window based on it using QUiLoader
. In your example, this would be something like:
import sys
from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication
main_form = QFile("MainWindow.ui")
main_form.open(QFile.ReadOnly)
app = QApplication(sys.argv)
window = QUiLoader().load(main_form)
window.show()
app.exec_()
Another option would be using the pyside6-uic
tool to generate a class for a widget based on your form file:
$ pyside6-uic MainWindow.ui > main_window.py
Then use it in your code like:
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from main_window import UI_MainWindow # your form class name with a UI_ prefix
app = QApplication(sys.argv)
window = QMainWindow()
UI_MainWindow().setupUi(window)
window.show()
app.exec_()