4
我有一個QComboBox,因此用戶可以從模型列中獲取網絡名稱。我正在使用這樣的代碼:向鏈接到模型的QComboBox添加「無」選項
self.networkSelectionCombo = QtGui.QComboBox()
self.networkSelectionCombo.setModel(self.model.worldLinks)
self.networkSelectionCombo.setModelColumn(WLM.NET_NAME)
我使用PySide,但這是一個真正的Qt問題。使用C++的答案很好。
我需要給用戶選擇不選擇任何網絡。我想要做的是在名爲'None'的組合框中添加一個額外的項目。但是這隻會被模型內容覆蓋。
我能想到的唯一方法是在這個模型列上創建一箇中間自定義視圖,並使用它來更新組合,然後視圖可以處理添加額外的'魔術'項目。有沒有人知道這樣做的更優雅的方式?
整潔。我會試一試。 – 2012-03-20 15:38:53
我實際上創建了一個新的QAbstractListModel子類,而不是我的主模型的子類。然後我將主模型傳遞給構造函數,以便新模型可以訪問現有模型的數據。儘管如此,這個答案讓我走上了正確的道路,在其他情況下,對原始模型類進行子類化可能會更好。公認。 – 2012-03-20 17:21:28
我很高興答案幫助你。 – pnezis 2012-03-20 19:00:26