aru*_*nte 6 python qt pyqt4 pyside python-2.7
所以我有一个应用程序,我正在考虑从 PyQt4 迁移到 PySide。在这个应用程序中,我.ui经常使用文件,使用模式如下:
class BaseGUIWidget(QWidget):
def __init__(self, parent = None, ui_file = None):
'''
:param parent: parent widget of this widget
:param ui_file: path to UI file to load (optional)
'''
super(BaseGUIWidget, self).__init__(parent)
if ui_file is not None:
uic.loadUi(ui_file, self)
Run Code Online (Sandbox Code Playgroud)
假设我有类似的班QFrame,QMainWindow,QGroupBox,等。
这允许我创建使用来自 UI 文件的数据的 Python 类,以及我在代码中手动添加的任何附加功能。本质上,我的BaseGUIWidget类就像扩展 UI 文件生成的类一样。应用程序中的许多功能都非常依赖于这种行为。
但是,据我所知,PySideQUIloader没有类似的功能。不是将 UI 文件的内容“填充”到您的类中,它只是在内部从 UI 文件构建一个小部件,然后返回它,然后您将它嵌入到您的小部件中,layout就像任何其他小部件一样。,IE:
class BaseGUIWidget(QWidget):
def __init__(self, parent = None, ui_file = None):
'''
:param parent: parent widget of this widget
:param ui_file: path to UI file to load (optional)
'''
super(BaseGUIWidget, self).__init__(parent)
self.setLayout(QVBoxLayout())
if ui_file is not None:
loader = QUILoader()
uifile = QFile(ui_file)
uifile.open(QFile.ReadOnly)
self.ui_widget = loader.load(ui_file, self)
self.layout().addWidget(self.ui_widget)
uifile.close()
Run Code Online (Sandbox Code Playgroud)
这是一个相当大的差异。例如,如果您希望您的 UI 文件包含 aQMainWindow并且您的 python 类仍然是 的扩展,QMainWindow因此它对其他类的行为就像一个,那么您最终会在 aQMainWindow内部QMainWindow,这不是您想要的。这也意味着您必须widget.ui_widget.XXX访问 UI 文件生成的小部件。
有没有办法让 PySide 的uic实现像 PyQt 的一样?
小智 -2
其实我也想知道同样的事情,只是尝试了这个:
将 uic 包从 PyQt4 安装复制到您的 PySide 包(它的所有 python 模块,没有编译的库,所以不应该有任何不兼容的情况)
对 uic 包进行搜索和替换,将“PyQt4”替换为“PySide”
用它:
from PySide import uic
w = uic.loadUi("mywidget.ui")
w.show()
Run Code Online (Sandbox Code Playgroud)它也适用于我所描述的用例 (uic.loadUi(ui_file, self)) :) 当然不能保证一切都会工作,但我很惊讶这个黑客让它工作。
| 归档时间: |
|
| 查看次数: |
1626 次 |
| 最近记录: |