本文介绍了PyQt 中的 QUILoader.createWidget 等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PySide 应用程序,我正在研究使其与 PyQt4 兼容.显然这应该不会太难,但我想尽量减少必要的代码更改次数.我遇到的最大区别(影响我的程序)是 PyQt 没有包装 QUiLoader 类.

I have a PySide application that I'm investigating making compatible with PyQt4. Obviously this shouldn't be too difficult, but I'd like to minimise the number of code changes necessary. The biggest difference I've come across (which affects my program) is the fact that PyQt does not wrap the QUiLoader class.

现在我意识到我仍然可以在 PyQt 中使用 uic 模块加载 ui 文件,但是我目前正在 PySide 中使用 QUiLoader 的子类,它实现了一些我想要的自定义功能如果可能,请保留.

Now I realise that I can still load ui files with the uic module in PyQt, however I'm currently using a subclass of QUiLoader in PySide that implements some custom functionality that I'd like to reserve if possible.

功能如下.我已经继承了 QUiLoader 并覆盖了 createWidget() 方法.我的新实现允许您在应用程序运行时动态注册小部件促销.我可以这样做,因为 createWidget() 方法负责从 ui 文件实例化小部件,之后(我相信)标准 QUiLoader 实现应用 ui 文件中的指定属性.

The functionality is as follows. I've subclassed QUiLoader and overridden the createWidget() method. My new implementation allows you to dynamically register widget promotions at application runtime. I can do this because the createWidget() method is responsible for instantiating the widgets from the ui file, after which (I believe) the standard QUiLoader implementation applies the specified properties from the ui file.

我的问题是,是否有类似的方法可以在 PyQt 中覆盖以在运行时动态设置小部件提升,而不必通过 Qt 设计器和 ui 文件静态设置它.

My Question is, is there a similar method I could override in PyQt to dynamically set widget Promotion at runtime, rather than having to set it statically through Qt Designer and the ui file.

附言我正在寻找对现有代码进行最少更改的解决方案.例如,如果我可以用一个在 PyQt 下提供等效行为的类替换我的 PySide QUiLoader 子类,那将是理想的.

P.S. I'm looking for a solution that involves minimal change to my existing code. For instance if I could replace my PySide QUiLoader subclass with a class that provides equivalent behaviour under PyQt, that would be ideal.

实现将使用类似:

loader = UiLoader()
loader.registerCustomPromotion("myWidgetNameInQtDesigner",ClassToPromoteTo)
ui = loader.load('myUiFile.ui')

这实际上是我在 PySide 中使用 QUiLoader 的子类所做的..ui 文件中名为 "myWidgetNameInQtDesigner" 的任何小部件都将被实例化为 ClassToPromoteTo 的实例.

This is effectively what I have made with my subclass of QUiLoader in PySide. Any widget in the .ui file with the name "myWidgetNameInQtDesigner" would be instantiated as an instance of ClassToPromoteTo.

推荐答案

事实证明这很容易,只要您愿意在 Qt Designer 中推广相关小部件.

This turns out to be quite easy, so long as you are willing to promote the relevant widgets in Qt Designer.

想法是在 sys.modules 中添加一个虚拟模块,然后动态修改它包含的自定义小部件类.

The idea is to add a dummy module to sys.modules and then dynamically modify the custom widget classes it contains.

因此,如果在 Qt Designer 中推广小部件时将头文件"设置为mylib.dummy",则在使用 PyQt 加载 ui 文件时会执行以下操作:

So if the "Header file" was set to "mylib.dummy" when promoting widgets in Qt Designer, you would do something like this when loading ui files with PyQt:

from types import ModuleType

# dummy module
module = sys.modules['mylib.dummy'] = ModuleType('dummy')

if mode == 'FOO':
    module.TextEdit = FooTextEdit
    module.LineEdit = FooLineEdit
    module.PushButton = FooPushButton
elif mode == 'BAR':
    module.TextEdit = BarTextEdit
    module.LineEdit = BarLineEdit
    module.PushButton = BarPushButton
else:
    module.TextEdit = QTextEdit
    module.LineEdit = QLineEdit
    module.PushButton = QPushButton

# loadUi/loadUiType will add the following import line:
# from mylib.dummy import TextEdit, LineEdit, PushButton
WidgetUI = uic.loadUiType('mywidget.ui'))[0]
ui = WidgetUI()

这篇关于PyQt 中的 QUILoader.createWidget 等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:43
查看更多