python-3.x - 在 Pyside/PyQt 中的 QWizard 页面之间共享变量

标签 python-3.x pyqt pyside

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

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

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

class MyCustomClass():

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

        ...set other attributes

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

每个匹配项都存储为 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在上一页中,它清楚地显示了所有内容。
  • 什么 我做错了吗registerField ?
  • 有没有在页面之间共享此类数据的更简单方法是什么?
  • 最佳答案

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

    就像我说的,我有一个匹配对象列表,其中每个匹配项都是一个名称列表,以及找到的与该名称对应的对象,即 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(未使用)。然后,在注册字段的时候,我没有放 Read 函数(readList),而是放了 QProperty 本身(match_list)。不知道它为什么会起作用,但可以想象这个可以用于注册其他自定义对象。

    关于python-3.x - 在 Pyside/PyQt 中的 QWizard 页面之间共享变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25586848/

    相关文章:

    python - PyQT 中显示 QDialog 后执行代码的惯用方法是什么?

    python - 在 numpy 数组中一次访问 block

    python-3.x - Python 3 - 将自定义 header 添加到 urllib.request 请求

    python - 如何通过多线程任务发出pyqtsignal

    python - PyQt4:QWidget 和 QMainWindow 的区别

    qt - 如何将带有事件/信号的自定义小部件用作 QStyledItemDelegates?

    python - 在设定的时间间隔内调用 QCoreApplications.processEvents() 安全吗?

    python - 按嵌套列表的最高值对字典进行排序

    python - 在谷歌应用引擎和 ndb 数据存储中从 python2.7 升级到 python3.7

    qt - PyQt:为现有数据库制作 CRUD UI 的最简单方法?