简体   繁体   English

推广PyQt Widget

[英]Promote PyQt Widget

I'm using PyQt and trying to promote a widget in QtDesigner. 我正在使用PyQt并试图在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. 如果我在“头文件”字段中指定包含我的窗口小部件子类的文件的完整模块路径,我能够使它工作。

Is there a better way to promote a widget in QtDesigner to a PyQt widget without having to specify the full module path? 有没有更好的方法将QtDesigner中的窗口小部件提升为PyQt窗口小部件而无需指定完整的模块路径?

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

In MainUi.ui I promote a widget and set the Header file field to: "PythonModuleRoot.Ui.CustomWidget". 在MainUi.ui中,我提升了一个小部件并将Header文件字段设置为:“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

So I just needed to change the contents of Header file to be how MainUi would import CustomWidget, so: "Widgets.CustomWidget". 所以我只需要将Header文件的内容更改为MainUi将如何导入CustomWidget,因此:“Widgets.CustomWidget”。 This article pointed me in the right direction: http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17893.html 本文向我指出了正确的方向: http//www.mail-archive.com/pyqt@riverbankcomputing.com/msg17893.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM