The code below creates a single QComboBox
. The combo's QStandardItem
s are set with data_obj
using setData
method. Changing combo
's current index triggers run
method which iterates combo
' and prints the data_obj
which turns to Python dictionary. How to make the data_obj
persistent?
app = QApplication(list())
class DataObj(dict):
def __init__(self, **kwargs):
super(DataObj, self).__init__(**kwargs)
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QVBoxLayout())
self.combo = QComboBox(self)
for i in range(5):
combo_item = QStandardItem('item_%s' % i)
data_obj = DataObj(foo=i)
print '..out: %s' % type(data_obj)
combo_item.setData(data_obj, Qt.UserRole + 1)
self.combo.model().appendRow(combo_item)
self.combo.currentIndexChanged.connect(self.run)
self.layout().addWidget(self.combo)
def run(self):
for i in range(self.combo.count()):
item = self.combo.model().item(i, 0)
data_obj = item.data(Qt.UserRole + 1)
print ' ...in: %s' % type(data_obj)
if __name__ == '__main__':
gui = Dialog()
gui.resize(400, 100)
gui.show()
qApp.exec_()
Below is the working solution to this problem: