【问题标题】:Sharing variable between QWizard pages in Pyside/PyQt在 Pyside/PyQt 中的 QWizard 页面之间共享变量
【发布时间】:2014-09-22 23:36:35
【问题描述】:

我正在 PySide 中构建一个 QWidget,但在尝试在页面之间共享数据时遇到了问题。

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

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

class MyCustomClass():

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

        ...set other attributes

在我的 QWizard 中,我打开一个文件并创建一个名称列表以匹配另一个 MyCustomClass 对象列表。然后,我在对应的MyCustomClass 对象的匹配name 旁边显示名称,并提示用户确认(或更改),然后再转到下一页。

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

首先我创建一个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 对象。

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 时清楚地显示了它们。

  1. 我在registerField 上做错了什么

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

【问题讨论】:

标签: python-3.x pyqt pyside


【解决方案1】:

其实我自己解决了。我在正确的轨道上,只是遗漏了一些东西,但我会在这里为其他有类似问题的人分类。

就像我说的那样,我有一个匹配对象列表,其中每个匹配项都是一个名称列表,以及与该名称对应的对象,即match = [name, MyCustomClass]

class ConfirmMatches(QWizardPage):

   # Function to change list
   def setList(self, new_list):

       self.list_val = new_list

       if self.list_val != []:
           self.list_changed.emit()

   # Function to return list
   def readList(self):

       return self.list_val

   def __init__(self):

       self.list_val = [] # Create initial value

       # Code to initialize displays/buttons, and generate matches
       ...

       # Here I assign the matches I made "matches", to the QProperty "match_list"
       self.setList(matches)

       # Then register field here.
       # Instead of the read function, I call the object itself (not sure why, but it works)
       self.registerField("registered_list", self, "match_list")

   # Define "match_list" as a QProperty with read and write functions, and a signal (not used)
   match_list = Property(list, readList, setList)
   listChanged = Signal()            

我将列表设为 QProperty,并编写了 Read 和 Write 函数以及 Signal(未使用)。然后,在注册字段时,我没有放置读取函数(readList),而是放置了 QProperty 本身(match_list)。不知道为什么它会起作用,但这个可以想象的可以用来注册其他自定义对象。

【讨论】:

  • 这是一个错字吗:self.list_changed.emit()?不应该是self.listChanged.emit()吗??
【解决方案2】:

如果您想在 ConfirmMatches 页面中明确设置字段 matches 的值,您需要执行以下操作之一:

  1. 只要匹配发生变化,就显式调用 self.setField。
  2. 在注册属性后每次更改匹配时都会发出信号
  3. 将匹配项存储在标准 Qt 输入之一中,例如 QLineEdit,并在 registerField 调用中使用该小部件。

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

【讨论】:

    猜你喜欢
    • 2014-08-29
    • 1970-01-01
    • 2013-04-22
    • 2018-02-13
    • 2022-11-30
    • 2018-12-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    相关资源
    最近更新 更多