在 PySide 中加载 QtDesigner 的 .ui 文件

发布于 2024-11-30 21:13:52 字数 91 浏览 1 评论 0原文

我正在寻找一个简单的示例,说明如何将 QtDesigner 生成的 .ui 文件直接加载到 Python 应用程序中。

我只是想避免使用 pyuic4。

I am looking for a simple example of how to directly load a QtDesigner generated .ui file into a Python application.

I simply would like to avoid using pyuic4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝海 2024-12-07 21:13:52

对于 PySide 和 .ui 文件的完整菜鸟,这里是一个完整的示例:

from PySide import QtCore, QtGui, QtUiTools


def loadUiWidget(uifilename, parent=None):
    loader = QtUiTools.QUiLoader()
    uifile = QtCore.QFile(uifilename)
    uifile.open(QtCore.QFile.ReadOnly)
    ui = loader.load(uifile, parent)
    uifile.close()
    return ui


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = loadUiWidget(":/forms/myform.ui")
    MainWindow.show()
    sys.exit(app.exec_())

For the complete noobs at PySide and .ui files, here is a complete example:

from PySide import QtCore, QtGui, QtUiTools


def loadUiWidget(uifilename, parent=None):
    loader = QtUiTools.QUiLoader()
    uifile = QtCore.QFile(uifilename)
    uifile.open(QtCore.QFile.ReadOnly)
    ui = loader.load(uifile, parent)
    uifile.close()
    return ui


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = loadUiWidget(":/forms/myform.ui")
    MainWindow.show()
    sys.exit(app.exec_())
別甾虛僞 2024-12-07 21:13:52

PySide 与 PyQt 不同,它实现了 QUiLoader 类来直接读取 .ui 文件。
从链接的文档中,

loader = QUiLoader()
file = QFile(":/forms/myform.ui")
file.open(QFile.ReadOnly)
myWidget = loader.load(file, self)
file.close()

PySide, unlike PyQt, has implemented the QUiLoader class to directly read in .ui files.
From the linked documentation,

loader = QUiLoader()
file = QFile(":/forms/myform.ui")
file.open(QFile.ReadOnly)
myWidget = loader.load(file, self)
file.close()
潦草背影 2024-12-07 21:13:52

另一种变体,基于较短的加载指令,可在 https://askubuntu.com/questions/140740/should-i-use-pyqt-or-pyside-for-a-new-qt-project#comment248297_141641。 (基本上,您可以避免所有文件打开和关闭。)

import sys
from PySide import QtUiTools
from PySide.QtGui import *

app = QApplication(sys.argv)
window = QtUiTools.QUiLoader().load("filename.ui")
window.show()
sys.exit(app.exec_())

注意:

  • filename.ui 应与您的 .py 文件位于同一文件夹中。
  • 您可能需要使用 if __name__ == "__main__":BarryPye 的回答中所述

Another variant, based on a shorter load directive, found on https://askubuntu.com/questions/140740/should-i-use-pyqt-or-pyside-for-a-new-qt-project#comment248297_141641. (Basically, you can avoid all that file opening and closing.)

import sys
from PySide import QtUiTools
from PySide.QtGui import *

app = QApplication(sys.argv)
window = QtUiTools.QUiLoader().load("filename.ui")
window.show()
sys.exit(app.exec_())

Notes:

  • filename.ui should be in the same folder as your .py file.
  • You may want to use if __name__ == "__main__": as outlined in BarryPye's answer
闻呓 2024-12-07 21:13:52

对于来自 PyQt5/6 的人来说,他们对此感到非常困惑:

PySide 没有我们习惯的相同功能,即在窗口/小部件子类顶部加载 ui 文件,如下所示:

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        
        '''Load ui'''
        uic.loadUi("mainwindow.ui", self)

没有任何东西与 PySide 中的非常相似。

相反,最好的办法是接受您已经避免的 ui 文件编译,因为在 PyQt 中加载 ui 文件非常容易。这有几个优点 有

  • 一个性能优势 - ui 文件不需要在运行时编译
  • 你可以获得所有小部件的类型提示,而无需手动添加它们

缺点是你必须使用 pyside6-uic每次编辑 *.ui 文件时都会对其进行编译,但是通过使用脚本来自动化该过程可以减轻痛苦 - 在 VSCode、批处理文件或 powershell 脚本中进行设置。完成此操作后,代码就很好了:

#ui_mainwindow is the ui_mainwindow.py file
#Ui_MainWindow is the class that was produced within that .py file
from ui_mainwindow import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        '''Load the ui'''
        self.setupUi(self)

For people coming from PyQt5/6 who are thoroughly confused by this:

PySide does not have the same functionality that we're used to, which is to load the ui file at the top of the window/widget subclass like so:

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        
        '''Load ui'''
        uic.loadUi("mainwindow.ui", self)

There is nothing very similar to this in PySide.

Instead, the best thing to do is embrace the ui-file compilation that you've avoided because loading the ui file is so easy in PyQt. This has a couple of advantages

  • There is a performance advantage - the ui file does not need to be compiled at run time
  • You get type hints for all your widgets without needing to manually add them

The disadvantage is that you have to use pyside6-uic to compile the *.ui files each time you edit them, but this can be made less painful by using scripts to automate the process - either setting it up in VSCode, a batch file or a powershell script. After you've done this, the code is nice:

#ui_mainwindow is the ui_mainwindow.py file
#Ui_MainWindow is the class that was produced within that .py file
from ui_mainwindow import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        '''Load the ui'''
        self.setupUi(self)

甩你一脸翔 2024-12-07 21:13:52

这是 PySide6 和 Windows 的一些示例。 (对于 Linux,您需要使用 /,而不是 \\

from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QFile
from PySide6.QtWidgets import QApplication
import sys

if __name__ == "__main__":
    app = QApplication(sys.argv)
    loader = QUiLoader()
    file = QFile("gui.ui")
    file.open(QFile.ReadOnly)
    ui = loader.load(file)
    file.close()
    ui.show()
    sys.exit(app.exec_())

Here is some example for PySide6 and Windows. (For linux you need use /, not \\)

from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QFile
from PySide6.QtWidgets import QApplication
import sys

if __name__ == "__main__":
    app = QApplication(sys.argv)
    loader = QUiLoader()
    file = QFile("gui.ui")
    file.open(QFile.ReadOnly)
    ui = loader.load(file)
    file.close()
    ui.show()
    sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文