【发布时间】:2015-11-11 21:45:19
【问题描述】:
而不是使用.addItem("Item Name", "My Data") 来填充QComboBox
我先创建它的项目:
item = QtGui.QStandardItem("Item Name")
然后我设置项目的数据:
item.setData("My data")
问题。如何从 currentIndexChanged() 方法内部获取存储在 Combo 的 Item 中的数据,该方法将单击的 ComboBox 项的索引作为参数:
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_())
【问题讨论】:
-
多余的问题,几天前你已经在这里得到了答案:stackoverflow.com/questions/31998023/…