2014-08-30 139 views
2

我在PySide中構建了一個QWidget,並在嘗試在頁面之間共享數據時遇到問題。在Pyside/PyQt中的QWizard頁面之間共享變量

總結我利用前面頁面的用戶輸入來構建一個自定義對象列表,我需要與下一頁共享。

在我的代碼開始,我建立一個自定義的對象,一個名爲.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. 有沒有一個更簡單的方法來分享這種類型的數據之間的頁面?

+0

您是否檢查過QWizard [示例](http://qt-project.org/doc/qt-4.8/qwizard.html#details)? – Trilarion 2014-09-03 08:43:08

回答

0

如果你要明確設置字段matches的價值在ConfirmMatches頁面,你需要做的幾件事情之一:

  1. 進行顯式調用self.setField任何你的比賽改變了。
  2. 發出信號,每次你的比賽正在註冊的財產
  3. Store的比賽中的Qt的標準輸入中的一個,像QLineEdit後改變時間,並使用該插件在registerField通話。

如果您檢查QWizardPage.registerField的文檔,它會做什麼是註冊以獲取傳入widget的指定屬性,當widget的信號發出時。現在您的代碼的方式,您需要添加一個信號到您的ConfirmMatches頁面,每當您的匹配變量發生變化時就會發出一個信號。否則,你的頁面不知道該字段何時應該更新。

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函數(readList),而是將QProperty本身(match_list)。不知道它爲什麼會起作用,但是這個可以想象的可以用來註冊其他自定義對象。

+0

這是一個錯字:self.list_changed.emit()?它不應該是self.listChanged.emit()? – user97662 2017-06-25 16:24:32