推广 PyQt Widget [英] Promote PyQt Widget

查看:41
本文介绍了推广 PyQt Widget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt 并尝试在 QtDesigner 中推广一个小部件.如果我在头文件"字段中指定包含我的小部件子类的文件的完整模块路径,我就能让它工作.

I'm using PyQt and trying to promote a widget in QtDesigner. I'm able to get it working if I specify the full module path, in the "Header file" field, to the file that contains my widget sub-class.

是否有更好的方法将 QtDesigner 中的小部件提升为 PyQt 小部件而无需指定完整的模块路径?

Is there a better way to promote a widget in QtDesigner to a PyQt widget without having to specify the full module path?

这里有一个例子,希望能说明我在说什么:

Here's an example to hopefully illustrate what I'm talking about:

/PythonModuleRoot/Ui/MainUi.py

/PythonModuleRoot/Ui/MainUi.py

from PyQt4 import QtCore, QtGui, uic
class MainUi(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.ui = uic.loadUi(os.path.join(os.path.dirname(__file__), 'MainUi.ui'), self)

/PythonModuleRoot/Ui/CustomWidget.py

/PythonModuleRoot/Ui/CustomWidget.py

from PyQt4 import QtCore, QtGui, uic
class CustomWidget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)

/PythonModuleRoot/Ui/MainUi.ui

/PythonModuleRoot/Ui/MainUi.ui

在 MainUi.ui 中,我提升了一个小部件并将头文件字段设置为:PythonModuleRoot.Ui.CustomWidget".

In MainUi.ui I promote a widget and set the Header file field to: "PythonModuleRoot.Ui.CustomWidget".

推荐答案

我明白了,我的实际代码与我给出的简化示例略有不同.我的实际代码更像是这样:

I got it figured out, my actual code is slightly different then the simplified example I gave. My actual code is more like this:

/PythonModuleRoot/Ui/MainUi.py

/PythonModuleRoot/Ui/MainUi.ui

/PythonModuleRoot/Ui/Widgets/CustomWidget.py

所以我只需要将头文件的内容更改为 MainUi 将如何导入 CustomWidget,因此:Widgets.CustomWidget".这篇文章为我指明了正确的方向:http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17893.html

So I just needed to change the contents of Header file to be how MainUi would import CustomWidget, so: "Widgets.CustomWidget". This article pointed me in the right direction: http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17893.html

这篇关于推广 PyQt Widget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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