我如何使用 QSqlTableModel 找到给定复合主键的 QModelIndex
我有这个表:
QSqlQuery createTblSMS("CREATE TABLE sms_tbl("
"isRead BOOLEAN NOT NULL,"
"readTime DATETIME,"
"arrivalTime DATETIME NOT NULL,"
"sender TEXT NOT NULL,"
"receiver TEXT NOT NULL,"
"smsContent TEXT,"
"PRIMARY KEY(arrivalTime, sender, receiver));", QSqlDatabase::database(mConnectionName));
smsModel = new QSqlTableModel(this, QSqlDatabase::database(mConnectionName));
smsModel->setTable("sms_tbl");
smsModel->select();
如何找到给定复合主键的 QModelIndex,例如:
"2010-08 12 12:04:15" "075588455" "077789585"
I have this table:
QSqlQuery createTblSMS("CREATE TABLE sms_tbl("
"isRead BOOLEAN NOT NULL,"
"readTime DATETIME,"
"arrivalTime DATETIME NOT NULL,"
"sender TEXT NOT NULL,"
"receiver TEXT NOT NULL,"
"smsContent TEXT,"
"PRIMARY KEY(arrivalTime, sender, receiver));", QSqlDatabase::database(mConnectionName));
smsModel = new QSqlTableModel(this, QSqlDatabase::database(mConnectionName));
smsModel->setTable("sms_tbl");
smsModel->select();
How can I find the QModelIndex
for given composite primary key, for example:
"2010-08 12 12:04:15" "075588455" "077789585"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我放弃了 QSqlTableModel。最好将 QAbstractTableModel 子类化并隐藏其中的所有 SQL 数据库访问。在这种情况下,您可以将复合主键保留在列表中,然后使用 QModelIndex.row() 引用主键的列表索引。
根据要求提供示例。代码未测试。
I gave up on QSqlTableModel. Better to subclass QAbstractTableModel and hide all the SQL database access inside that. In this case you'd keep your composite primary keys in a list and then use QModelIndex.row() to reference the list index of your primary key.
Examples as requested. Code not tested.