如何使用PySide2在QWizardPage中加载Qt设计器文件(.ui) [英] How to load a Qt Designer file (.ui) in a QWizardPage using PySide2

查看:563
本文介绍了如何使用PySide2在QWizardPage中加载Qt设计器文件(.ui)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Qt Designer中设计QWizardPages,并希望使用PySide2将它们加载到我的Python程序中.以前,我一直在使用PyQt5,没有任何问题,但是切换到PySide2似乎比预期的要难. 我面临的问题是,当我将QWizardPage添加到QWizard时,该页面的确添加到了向导中,而且还添加了另一个(空)页面.我找不到我在做什么错,所以我想知道是否有人可以看看.

I want to design my QWizardPages in Qt Designer and I want to load them into my Python program with PySide2. Previously I have been using PyQt5 without any problems but making the switch to PySide2 seems harder then expected. The problem I am facing is that when I am adding a QWizardPage to my QWizard , the page is indeed added to the Wizard, but also an other (empty) page is added. I'm not able to find what I'm doing wrong so I was wondering if someone can have a look.

我尝试添加具有功能addPage()和setPage()的页面,但是它们给出的结果相同.我还注意到的是,当我使用setTitle()显式设置页面的标题时,空白的(不需要的)页面将获得此标题,而不是我在Qt Designer中设计的页面.

I have tried to add the pages with both the functions addPage() and setPage(), but they give the same results. What I also noticed is that when I explicitely set the Title of the page with setTitle(), the empty (unwanted) page gets this title, but not the page I designed in Qt Designer.

import os
import sys
from PySide2.QtWidgets import QWizard, QWizardPage, QApplication
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
from enum import Enum


class MyWizard(QWizard):

    def __init__(self):
        super().__init__()

        self.setPage(PageNumbers.page_one.value, PageOne(self))


class PageOne(QWizardPage):

    def __init__(self, parent):
        super().__init__(parent)

        ui_file = os.path.join(__file__, '..', 'pageOne.ui')
        file = QFile(ui_file)
        file.open(QFile.ReadOnly)
        loader = QUiLoader()
        loader.load(file, parent)
        file.close()
        self.setTitle("This is another test Title")


class PageNumbers(Enum):
    page_one = 1


if __name__ == '__main__':

    app = QApplication(sys.argv)

    wizard = MyWizard()
    wizard.show()

    app.exec_()

我希望只有一个QWizardPage出现,并直接带有完成"按钮.相反,我得到两个QWizardPages,如下图所示:

What I would expect is to have just one QWizardPage showing up with directly the Finish button. Instead I get two QWizardPages as shown in this image:

有人可以告诉我怎么回事吗?

Can someone tell me what's going on?

(我使用带有以下代码的PyQt5获得了预期的结果: https://pastebin.com/6W2sx9M1)

(I get the expected result using PyQt5 with the following code: https://pastebin.com/6W2sx9M1)

推荐答案

PyQt的开发人员实现的功能能够根据默认情况下未在Qt中实现的.ui创建类. MOC可以完成此工作),但是在PySide2-Qt for python的情况下,它没有实现,仅具有QUiLoader类,该类允许基于.ui创建窗口小部件,而与PyQt允许填充类

The developers of PyQt implement functions to be able to create classes based on the .ui that is not implemented in Qt by default (Qt/C++ uses the MOC to do this work), but in the case of PySide2-Qt for python it does not implement it, only has the QUiLoader class that allows to create a widget based on the .ui unlike PyQt that allows filling a class.

总而言之,loadUi函数的PySide2中没有等效项,因此您不能实现相同的逻辑. PySide2不是PyQt5,因为它们使用相同的基础,所以它们有自己的等效项,但是它们具有实现,局限性和优势.

In conclusion there is no equivalent in PySide2 of the loadUi function so you can not implement the same logic. PySide2 is not PyQt5, there are own equivalences since they use the same base but they have implementations, limitations and advantages.

转到实际问题,考虑到.ui在.py旁边,解决方案如下:

Going to the practical problem, considering that the .ui is next to the .py the solution is the following:

import os
import sys
from PySide2 import QtCore, QtWidgets, QtUiTools
from enum import Enum

class PageNumbers(Enum):
    page_one = 0

class MyWizard(QtWidgets.QWizard):
    def __init__(self):
        super().__init__()
        ui_file = os.path.join(os.path.dirname(os.path.abspath(__file__)) ,'PageOne.ui')
        page_one = create_widget(ui_file, self)
        self.setPage(PageNumbers.page_one.value, page_one)

def create_widget(filename, parent=None):
    file = QtCore.QFile(filename)
    if not file.open(QtCore.QFile.ReadOnly):
        return
    loader = QtUiTools.QUiLoader()
    widget = loader.load(file, parent)
    return widget

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    wizard = MyWizard()
    wizard.show()
    sys.exit(app.exec_())

这篇关于如何使用PySide2在QWizardPage中加载Qt设计器文件(.ui)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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