UI Module without .ui
file
There is a different between inheriting from QWidget
class vs QMainWindow
Inheriting from QWidget
:
1 | class InheritQWidget(QtWidgets.QWidget): |
Inheriting from QMainWindow
1 | class InheritQMainWindow(QtWidgets.QMainWindow): |
Custom Dialog
Sometimes we need a quick window to display some information, but the built-in qt message boxes aren’t suitable for the job.
Custom Widget
We can choose to create a widget class, but also for saving time, we can create a temporary custom widget
example:
Couple of things worth noting:
-
correctly initialize the widget
self.customWidget = QtWidgets.QWidget()
will allow widget be child of the main window, thus allow widget to close when main window is closedso not
customWidget = QtWidgets.QWidget()
orself.customWidget = QtWidgets.QWidget(self)
-
use
getattr
in combination withQStyle
and name of the built-in icon -
use
win.setAttribute(QtCore.Qt.WA_DeleteOnClose)
to make sure the child widget is killed after main window is closed, because default close only hides window objects -
self.customWidget.show()
is the core command to call it to display
Custom QMessageBox
add custom buttons to the QMessageBox
layout
1 | dialog = QtWidgets.QMessageBox() |
Don’t use the return value of QMessageBox::exec
, as it only makes sense
for standard buttons. Also don’t rely on buttonRole
as multiple buttons could be sharing the same role.