我如何使用 QSqlTableModel 找到给定复合主键的 QModelIndex

发布于 2024-09-14 04:46:21 字数 654 浏览 9 评论 0原文

我有这个表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2024-09-21 04:46:22

我放弃了 QSqlTableModel。最好将 QAbstractTableModel 子类化并隐藏其中的所有 SQL 数据库访问。在这种情况下,您可以将复合主键保留在列表中,然后使用 QModelIndex.row() 引用主键的列表索引。

根据要求提供示例。代码未测试。

NAME = 0    # Name column in db is model column 0
EMAIL = 1   # Email address column in db is model column 1

class MyModel(QAbstractTableModel):
    def __init__(self):
        self.rowrefs = []

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid() or \
                 not (0 <= index.row() < len(self.tableref)):
            return QVariant() # if index not valid return empty QVariant
        myref = self.rowref[index.row()]
        column = index.column()
        # insert code to look up the db row based on the row id in myref
        # and put data in a list 'dbrow' with list indexes matching model column numbers
        if role == Qt.DisplayRole:
            if column = NAME:
                return QVariant(dbrow[NAME])
            elif column = EMAIL:
                return QVariant(dbrow[EMAIL])
        etc.....

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.

NAME = 0    # Name column in db is model column 0
EMAIL = 1   # Email address column in db is model column 1

class MyModel(QAbstractTableModel):
    def __init__(self):
        self.rowrefs = []

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid() or \
                 not (0 <= index.row() < len(self.tableref)):
            return QVariant() # if index not valid return empty QVariant
        myref = self.rowref[index.row()]
        column = index.column()
        # insert code to look up the db row based on the row id in myref
        # and put data in a list 'dbrow' with list indexes matching model column numbers
        if role == Qt.DisplayRole:
            if column = NAME:
                return QVariant(dbrow[NAME])
            elif column = EMAIL:
                return QVariant(dbrow[EMAIL])
        etc.....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文