使用 QtSql 模型设置组合框模型会出错
Setting up combobox model with QtSql model gives an error
我有以下代码:
uic.loadUi("mainwindow.ui", self)
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('people.db')
self.db.open()
try:
if self.db.isOpen():
print('DB open')
# self.pushButtonClear.clicked.connect(self.clearFields)
# self.pushButtonSave.clicked.connect(self.insertRowToModel)
self.pushButtonDelete.clicked.connect(self.deleteTableRow)
self.model = QSqlRelationalTableModel(db=self.db)
self.model.setTable("person")
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.group_index = self.model.fieldIndex("GroupId") #foreign key
self.model.setRelation(self.group_index, QSqlRelation("Groups", "GroupId", "GroupName"))
self.model.select()
self.relModel = self.model.relationModel(self.group_index)
self.comboBoxGroup.setModel(self.relModel)
self.comboBoxGroup.setModelColumn(self.relModel.fieldIndex("GroupName"))
self.tableView.setModel(self.model)
self.mapper = QDataWidgetMapper()
self.mapper.setModel(self.model)
self.mapper.setItemDelegate(QSqlRelationalDelegate(self))
self.mapper.addMapping(self.lineEditId, 0)
self.mapper.addMapping(self.lineEditForename, 1)
self.mapper.addMapping(self.lineEditSurename, 2)
self.mapper.addMapping(self.dateEditBirthday, 3)
self.mapper.addMapping(self.lineEditCity, 4)
self.mapper.addMapping(self.comboBoxGroup, self.group_index)
self.mapper.toFirst()
except:
self.db.close()
print('Exception raised')
行内:self.comboBoxGroup.setModelColumn(self.relModel.fieldIndex("GroupName"))
它给出了一个 AttributeError: 'NoneType' object has no attribute 'fieldIndex'
因为 self.relModel = self.model.relationModel(self.group_index) 导致 None 类型。无法设置。当我将列索引作为这样的数字输入时:self.comboBoxGroup.setModelColumn(self.relModel.fieldIndex(1)) 数据至少会显示,但组合框未填充。这里出了什么问题?
我发现了问题:
self.group_index = self.model.fieldIndex("group") #外键
字段名称不正确。现在它可以工作,但只在有的地方提供条目
组中的条目 table(内部联接)
我有以下代码:
uic.loadUi("mainwindow.ui", self)
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('people.db')
self.db.open()
try:
if self.db.isOpen():
print('DB open')
# self.pushButtonClear.clicked.connect(self.clearFields)
# self.pushButtonSave.clicked.connect(self.insertRowToModel)
self.pushButtonDelete.clicked.connect(self.deleteTableRow)
self.model = QSqlRelationalTableModel(db=self.db)
self.model.setTable("person")
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.group_index = self.model.fieldIndex("GroupId") #foreign key
self.model.setRelation(self.group_index, QSqlRelation("Groups", "GroupId", "GroupName"))
self.model.select()
self.relModel = self.model.relationModel(self.group_index)
self.comboBoxGroup.setModel(self.relModel)
self.comboBoxGroup.setModelColumn(self.relModel.fieldIndex("GroupName"))
self.tableView.setModel(self.model)
self.mapper = QDataWidgetMapper()
self.mapper.setModel(self.model)
self.mapper.setItemDelegate(QSqlRelationalDelegate(self))
self.mapper.addMapping(self.lineEditId, 0)
self.mapper.addMapping(self.lineEditForename, 1)
self.mapper.addMapping(self.lineEditSurename, 2)
self.mapper.addMapping(self.dateEditBirthday, 3)
self.mapper.addMapping(self.lineEditCity, 4)
self.mapper.addMapping(self.comboBoxGroup, self.group_index)
self.mapper.toFirst()
except:
self.db.close()
print('Exception raised')
行内:self.comboBoxGroup.setModelColumn(self.relModel.fieldIndex("GroupName"))
它给出了一个 AttributeError: 'NoneType' object has no attribute 'fieldIndex' 因为 self.relModel = self.model.relationModel(self.group_index) 导致 None 类型。无法设置。当我将列索引作为这样的数字输入时:self.comboBoxGroup.setModelColumn(self.relModel.fieldIndex(1)) 数据至少会显示,但组合框未填充。这里出了什么问题?
我发现了问题: self.group_index = self.model.fieldIndex("group") #外键
字段名称不正确。现在它可以工作,但只在有的地方提供条目 组中的条目 table(内部联接)