I\'m searching for a way to use custom widgets in Qt Designer written with Qt for Python (PySide2) effectively.
I found out, that it is possible to design the GUI wi
There are 2 main ways to use a custom widget in Qt Designer:
This is the simplest and least laborious way, if it's an internal widget you just have to right click on the widget and select Promote To ..., then in:
What the above does is generate the following:
...
...
RadialBar
QWidget
radialbar.h
1
...
That is, change class, and add a field in customwidget pointing to the name of the class:
, name of the class they inherit
and file path changing the file extension from .py to .h
, that is, the same data as You entered the form.
In the case of the root widget it can not be done through Qt Designer, but I see that you have understood the logic but you have not modified everything correctly, your main mistake is to point out the class from which they inherit
MyMainWindow
QMainWindow <----
mymainwindow.h
1
So the correct file is:
custom_widget_modified.ui
MainWindow
0
0
800
600
MainWindow
0
0
155
15
-
TextLabel
-
TextLabel
-
TextLabel
0
0
800
21
File
0
0
3
18
Exit
MyMainWindow
QMainWindow
mymainwindow.h
1
actionExit
triggered()
MainWindow
close()
-1
-1
399
299
mymainwindow.py
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QMainWindow, QMessageBox
from PySide2.QtCore import QFile
class MyMainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("Demo QtWidget App")
def closeEvent(self, event):
msgBox = QMessageBox()
msgBox.setWindowTitle("Quit?")
msgBox.setText("Exit application?")
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msgBox.setDefaultButton(QMessageBox.No)
ret = msgBox.exec_()
if ret == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication([])
file = QFile("custom_widget_modified.ui")
file.open(QFile.ReadOnly)
loader = QUiLoader()
loader.registerCustomWidget(MyMainWindow)
main_window = loader.load(file)
main_window.show()
sys.exit(app.exec_())