我正在 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/