Customising code of Qt designer widget?

2019-01-09 17:21发布

问题:

I need to add some features to a graphics widget in a form I created using the Qt Designer.

For example I would normally do something like this:

class custom_gv(QGraphicsView):
    def __init__(self):
        super().__init__()

    def zoom(self):
        # custom code here

But in this case the graphics view is a part of the window I made in Qt Designer. I know you can use the "promote to" feature in Qt designer but I don't know how to utilise that in code, especially considering that I use this method to use Qt Designer windows:

from PyQt5.uic import loadUiType

custom_window = loadUiType('ui.ui')

class Window(QMainWindow, custom_window):
    def __init__(self):
        QMainWindow.__init__(self)
        custom_window.__init__(self)
        self.setupUi(self)

So how would I go about customising the code of the graphics view in my window when I use Qt Designer?

回答1:

The most common way to solve this is by using widget promotion. This will allow you to replace a widget defined in Qt Designer with your own custom class. The steps for doing this are as follows:

In Qt Designer, select the QGraphicsView you want to replace, then right-click it and select Promote to... . In the dialog, set Promoted class name to "custom_gv", and set Header file to the python import path for the module that contains this class (e.g. "mypkg.widgets"). Then click Add, and Promote, and you will see the class change from "QGraphicsView" to "custom_gv" in the Object Inspector pane.

When the Qt Designer ui file is converted into PyQt code, it will automatically add an import statement like this:

from mypkg.widgets import custom_gv

and then in the converted code it will replace something like this:

    self.graphicsView = QtWidgets.QGraphicsView(MainWindow)

with this:

    self.graphicsView = custom_gv(MainWindow)

So the code in the ui file knows nothing about the custom class: it's just a name that is imported from elsewhere. That means you are completely free to write the custom class in any way you like.

In PyQt, this mechanism works in the same way with pyuic as it does with the uic module. The loadUi and loadUiType functions generate exactly the same code as pyuic does. The only difference is that the pyuic tool writes the generated code to a file, whereas the uic module loads it directly via exec.