4

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?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
John O'Neil
  • 355
  • 1
  • 5
  • 15
  • Promoting widgets is [quite simple](http://stackoverflow.com/a/22543079/984421). What specifically don't you understand about it? – ekhumoro Feb 05 '17 at 19:10
  • How I use it in PyQt when using the LoadUiType method instead of the convert to code first method – John O'Neil Feb 06 '17 at 04:02
  • It works in exactly the same way. – ekhumoro Feb 06 '17 at 04:19
  • How do I edit the promoted class though? This may be because I don't really understand how tloadUiType works – John O'Neil Feb 06 '17 at 05:57
  • Possible duplicate of [PyQt promoting a widget in Qt Designer and using it with the loadUiType function](http://stackoverflow.com/questions/42074373/pyqt-promoting-a-widget-in-qt-designer-and-using-it-with-the-loaduitype-function) – eyllanesc Feb 06 '17 at 18:26

1 Answers1

6

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336