自定义 Qt 设计器小部件的代码? [英] Customising code of Qt designer widget?

查看:72
本文介绍了自定义 Qt 设计器小部件的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我使用 Qt 设计器创建的表单中向图形小部件添加一些功能.

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

但在这种情况下,图形视图是我在 Qt Designer 中制作的窗口的一部分.我知道你可以在 Qt 设计器中使用提升到"功能,但我不知道如何在代码中使用它,特别是考虑到我使用这种方法来使用 Qt 设计器窗口:

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)

那么,当我使用 Qt Designer 时,如何自定义窗口中图形视图的代码?

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

推荐答案

最常见的解决方法是使用 widget 推广.这将允许您用您自己的自定义类替换 Qt Designer 中定义的小部件.执行此操作的步骤如下:

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:

在 Qt Designer 中,选择要替换的 QGraphicsView,然后右键单击它并选择 Promote to... .在对话框中,将Promoted class name设置为custom_gv",并将Header file设置为包含此类的模块的python导入路径(例如mypkg.widgets").然后点击AddPromote,你会在Object Inspector面板中看到类从QGraphicsView"变成了custom_gv".

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.

Qt Designer的ui文件转成PyQt代码后,会自动添加这样的import语句:

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)

这样:

    self.graphicsView = custom_gv(MainWindow)

所以 ui 文件中的代码对自定义类一无所知:它只是从其他地方导入的名称.这意味着您可以完全自由地以任何您喜欢的方式编写自定义类.

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.

在 PyQt 中,此机制与 pyuic 的工作方式与 uic 模块的工作方式相同.loadUiloadUiType 函数生成与 pyuic 完全相同的代码.唯一的区别是 pyuic 工具将生成的代码写入文件,而 uic 模块直接通过 exec.

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.

这篇关于自定义 Qt 设计器小部件的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆