我有一个基本组合框,其中填充了来自 sqlite3 数据库表的数据:
self.langModel = QSqlTableModel(self)
self.langModel.setTable("languages")
self.langModel.setSort(self.langModel.fieldIndex("label"), Qt.AscendingOrder)
self.langModel.select()
...
self.comboLangs = QComboBox()
self.comboLangs.setModel(self.langModel)
self.comboLangs.setModelColumn(self.langModel.fieldIndex("label"))
self.layout.addRow("Language", self.comboLangs)
...
self.btnAdd = QPushButton("Add")
self.btnAdd.clicked.connect(self.addLoc)
self.btnLayout.addWidget(self.btnAdd)
...
def addLoc(self):
pprint(vars(self.langModel.data(self.langModel.index(self.comboLangs.currentIndex(), self.langModel.fieldIndex("iso")))))
这是表架构:
CREATE TABLE languages (
id INTEGER PRIMARY KEY NOT NULL,
iso TEXT NOT NULL,
label TEXT NOT NULL
);
这只会产生一个空的 dict {}
。如何从 QSqlTableModel 中获取选定/当前项的数据对象?