如何使用 PySide 将 .ui 文件加载到 python 类上? [英] How do you load .ui files onto python classes with PySide?

查看:29
本文介绍了如何使用 PySide 将 .ui 文件加载到 python 类上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PyQt 已经有一段时间了,在我使用它的整个过程中,有一个非常一致的编程模式.

I've used PyQt for quite a while, and the entire time I've used it, there has been a pretty consistent programming pattern.

  1. 使用 Qt Designer 创建 .ui 文件.
  2. 创建一个与您在 .ui 文件中创建的小部件类型相同的 Python 类.
  3. 初始化python类时,使用uic将.ui文件动态加载到类中.

有没有办法在 PySide 中做类似的事情?我已经通读了文档和示例,我能找到的最接近的是一个计算器示例,它将 .ui 文件预渲染为 python 代码,这是在 PyQt 中执行它的超旧方法(为什么将它烘焙到 python什么时候可以解析用户界面?)

Is there any way to do something similar in PySide? I've read through the documentation and examples and the closest thing I could find was a calculator example that pre-rendered the .ui file out to python code, which is the super old way of doing it in PyQt (why bake it to python when you can just parse the ui?)

推荐答案

我正在使用 PySide 做到这一点.:)

I'm doing exactly that with PySide. :)

您使用这个 https://gist.github.com/cpbotha/1b42a20c8f3eb9bb7cb8(原始作者 Sebastian Wiesner 在 https://github.com/lunaryorn/snippets/blob/master/qt4/designer/pyside_dynamic.py 但已经消失) - 它覆盖了 PySide.QtUiTools.QUiLoader 并提供了一个新的 loadUi() 方法,这样你就可以做到这一点:

You use this https://gist.github.com/cpbotha/1b42a20c8f3eb9bb7cb8 (original by Sebastian Wiesner was at https://github.com/lunaryorn/snippets/blob/master/qt4/designer/pyside_dynamic.py but has disappeared) - which overrides PySide.QtUiTools.QUiLoader and supplies a new loadUi() method so that you can do this:

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        loadUi('mainwindow.ui', self)

当您实例化 MyMainWindow 时,它将拥有您使用 Qt 设计器设计的 UI.

When you instantiate MyMainWindow, it will have the UI that you designed with the Qt Designer.

如果您还需要使用自定义小部件(Qt Designer 中的Promote To"),请参阅此答案:https://stackoverflow.com/a/14877624/532513

If you also need to use custom widgets ("Promote To" in Qt Designer), see this answer: https://stackoverflow.com/a/14877624/532513

这篇关于如何使用 PySide 将 .ui 文件加载到 python 类上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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