2015-08-18 1 views
-2

Au lieu d'utiliser .addItem("Item Name", "My Data") pour alimenter le QComboBoxComment obtenir QComboBox données d'article

Je crée son article premier:

item = QtGui.QStandardItem("Item Name")

Je mis les données de l'article:

item.setData("My data")

Question. Comment obtenir les données stockées dans l'élément de Combo à l'intérieur de la méthode currentIndexChanged() qui obtient l'indice de l'élément ComboBox cliquée comme argument:

import sys 
import PySide.QtCore as QtCore 
import PySide.QtGui as QtGui 

class MyCombo(QtGui.QWidget): 
    def __init__(self, *args): 
     QtGui.QWidget.__init__(self, *args) 
     vLayout=QtGui.QVBoxLayout(self) 
     self.setLayout(vLayout) 

     self.combo=QtGui.QComboBox(self) 
     self.combo.currentIndexChanged.connect(self.currentIndexChanged) 
     comboModel=self.combo.model() 
     for i in range(3): 
      item = QtGui.QStandardItem(str(i)) 
      item.setData('MY DATA' + str(i)) 
      comboModel.appendRow(item) 
     vLayout.addWidget(self.combo) 

    def currentIndexChanged(self, index): 
     print index   

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    w = MyCombo() 
    w.show() 
    sys.exit(app.exec_()) 
+0

question redondante, vous avez obtenu la réponse il y a déjà quelques jours ici: http://stackoverflow.com/questions/31998023/qcombobox-and -app-setstylecleanlooks –

Répondre

2
import sys 
from PySide import QtGui, QtCore 

class MyCombo(QtGui.QWidget): 
    def __init__(self, *args): 
     QtGui.QWidget.__init__(self, *args) 
     vLayout=QtGui.QVBoxLayout(self) 
     self.setLayout(vLayout) 

     self.combo=QtGui.QComboBox(self) 
     self.combo.currentIndexChanged.connect(self.currentIndexChanged) 
     comboModel=self.combo.model() 
     for i in range(3): 
      item = QtGui.QStandardItem(str(i)) 
      comboModel.appendRow(item) 
      self.combo.setItemData(i,'MY DATA' + str(i)) 
     vLayout.addWidget(self.combo) 

    def currentIndexChanged(self, index): 
     print self.combo.itemData(index)  

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    w = MyCombo() 
    w.show() 
    sys.exit(app.exec_()) 

Cela devrait travailler pour vous, je pense que

+0

Fonctionne aussi! Merci! – alphanumeric

0

solution de travail est affiché ci-dessous. En utilisant la méthode item.setData(), nous devons spécifier un Role avec lequel nous associons les données.

class MyCombo(QtGui.QWidget): 
    def __init__(self, *args): 
     QtGui.QWidget.__init__(self, *args) 
     vLayout=QtGui.QVBoxLayout(self) 
     self.setLayout(vLayout) 

     self.combo=QtGui.QComboBox(self) 
     self.combo.currentIndexChanged.connect(self.currentIndexChanged) 
     comboModel=self.combo.model() 
     for i in range(3): 
      item = QtGui.QStandardItem(str(i)) 
      item.setData('MY DATA' + str(i), QtCore.Qt.UserRole) 
      comboModel.appendRow(item) 
     vLayout.addWidget(self.combo) 

    def currentIndexChanged(self, index):  
     modelIndex=self.combo.model().index(index,0) 
     print self.combo.model().data(modelIndex, QtCore.Qt.UserRole) 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    w = MyCombo() 
    w.show() 
    sys.exit(app.exec_())