在 Pyside/PyQt 中的 QWizard 页面之间共享变量 [英] Sharing variable between QWizard pages in Pyside/PyQt

查看:20
本文介绍了在 Pyside/PyQt 中的 QWizard 页面之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 PySide 中构建 QWidget,并在尝试在页面之间共享数据时遇到问题.

I am building a QWidget in PySide, and running into an issue when trying to share data between pages.

总而言之,我利用之前页面中的用户输入来构建自定义对象列表,我需要与以下页面共享.

To summarize I utilize user inputs from earlier pages to construct a list of custom objects, which I need to share with the following page.

在我的代码的开头,我构造了一个自定义对象,带有一个名为 .name(以及其他属性)的属性

At the beginning of my code I construct a custom object, with an attribute called .name (among other attributes)

class MyCustomClass():

    def __init__(self, name, other_attributes)
        self.name = name

        ...set other attributes

在我的 QWizard 中,我打开一个文件并制作一个名称列表以与另一个 MyCustomClass 对象列表匹配.然后,在相应 MyCustomClass 对象的匹配 name 旁边显示名称,并在移至下一页之前提示用户确认(或更改).

In my QWizard I open a file and make a list of names to match with another list of MyCustomClass objects. I then display the names alongside the matched name of the corresponding MyCustomClass object and prompt the user to confirm (or change), before moving to the next page.

每个匹配项都存储为一个 tuple(name, MyCustomClass) 并添加到列表中.然后我希望从下一页阅读此列表以执行更多操作.我正在尝试使用 .registerField,但我不确定如何正确使用.我的尝试如下.

Each match is stored as a tuple(name, MyCustomClass) and added to a list. I then wish to read this list from the next page in order to perform more operations. I'm trying to use .registerField, but I'm unsure of how to properly do so. My attempt is below.

首先我制作一个 QWizardPage,执行一些代码,然后构建我的匹配项.我做了一个函数来返回值并将其用于 .registerField

class ConfirmMatches(QWizardPage):

    def __init__(self):
        ...

    def initializePage(self):

        # Code to make display and operations and make list of matches
        ...
        self.matches = matches

        self.registerField("matches", self, "get_matches")

    def get_matches(self):
        return self.matches

然后从我的下一页开始,我尝试调用该字段,但我只返回一个 None 对象.

Then from my next page, I try to call the field, but I only return a None object.

class NextPage(QWizardPage):

    def __init__(self):
        ...

    def initializePage(self):

        # Get relevant fields from past pages

        past_matches = self.field("matches")

type(past_matches)None,即使我在上一页 print self.matches 时它清楚地显示了它们.

type(past_matches) is None, even though when I print self.matches in the previous page it clearly displays them all.

  1. 我在 registerField 上做错了什么?

  1. What am I doing wrong with the registerField?

是否有更简单的方法可以在页面之间共享此类数据?

Is there an easier way to share this type of data between pages?

推荐答案

如果您想在 ConfirmMatches 页面中显式设置字段 matches 的值,您可以需要做以下几件事之一:

If you want to explicitly set the value of the field matches in your ConfirmMatches page, you would need to do one of a few things:

  1. 在匹配项发生变化时明确调用 self.setField.
  2. 每次在注册财产后更改匹配项时发出信号
  3. 将您的匹配项存储在标准 Qt 输入之一中,例如 QLineEdit,并在 registerField 调用中使用该小部件.
  1. Make an explicit call to self.setField any time your matches change.
  2. Emit a signal every time your matches are changed after registering the property
  3. Store your matches in one of the standard Qt inputs, like QLineEdit, and use that widget in the registerField call.

如果您查看 QWizardPage.registerField,它的作用是注册以在发出小部件的信号时获取传入的小部件的命名属性.按照您现在的代码方式,您需要向 ConfirmMatches 页面添加一个信号,该信号将在您的匹配变量更改时发出.否则,您的页面不知道何时应该更新该字段.

If you check the docs for QWizardPage.registerField, what it does is register to grab the named property of the passed in widget when the widget's signal is emitted. The way your code is now, you would need to add a signal to your ConfirmMatches page that would be emitted whenever your matches variable changes. Otherwise, your page doesn't know when the field should be updated.

这篇关于在 Pyside/PyQt 中的 QWizard 页面之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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