QListView 中的 CheckBox 使用 QSqlTableModel

CheckBox in QListView using QSqlTableModel

我正在学习 PyQt5 框架并尝试构建一个项目的 QListView,每个项目前面都有一个复选框。我读过的大多数示例都没有说明如何使用 QSqlTableModel 完成此操作。有人可以给我指出正确的方向吗?

db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("./Data/mydbase.db")
db.open()
model = QSqlTableModel()
model.setTable("labdata")
model.setSort(1,0)
model.select()
while model.canFetchMore():
    model.fetchMore()
self.ui.lst_LabData.setModel(model)
self.ui.lst_LabData.setModelColumn(1)
self.ui.lst_LabData.show()

一个可能的选择是启用标记 Qt::ItemIsUserCheckable,并在角色为 Qt::CheckStateRole 时覆盖 data()setData() 方法,以存储我们创建的数据字典。

class TableModel(QSqlTableModel):
    def __init__(self, *args, **kwargs):
        QSqlTableModel.__init__(self, *args, **kwargs)
        self.checkeable_data = {}

    def flags(self, index):
        fl = QSqlTableModel.flags(self, index)
        if index.column() == 1:
            fl |= Qt.ItemIsUserCheckable
        return fl

    def data(self, index, role=Qt.DisplayRole):
        if role == Qt.CheckStateRole and (
            self.flags(index) & Qt.ItemIsUserCheckable != Qt.NoItemFlags
        ):
            if index.row() not in self.checkeable_data.keys():
                self.setData(index, Qt.Unchecked, Qt.CheckStateRole)
            return self.checkeable_data[index.row()]
        else:
            return QSqlTableModel.data(self, index, role)

    def setData(self, index, value, role=Qt.EditRole):
        if role == Qt.CheckStateRole and (
            self.flags(index) & Qt.ItemIsUserCheckable != Qt.NoItemFlags
        ):
            self.checkeable_data[index.row()] = value
            self.dataChanged.emit(index, index, (role,))
            return True
        return QSqlTableModel.setData(self, index, value, role)

示例:

def createConnection():
    db = QSqlDatabase.addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");
    if not db.open():
        print("Cannot open database"),
        print("Unable to establish a database connection.\n"
                        "This example needs SQLite support. Please read "
                        "the Qt SQL driver documentation for information how "
                        "to build it.\n\n"
                        "Click Cancel to exit.")
        return False

    query = QSqlQuery()
    query.exec_("create table person (id INTEGER PRIMARY KEY AUTOINCREMENT, "
               "firstname VARCHAR(20), lastname VARCHAR(20))");
    query.exec_("insert into person(firstname, lastname) values('Danny', 'Young')");
    query.exec_("insert into person(firstname, lastname) values('Christine', 'Holand')");
    query.exec_("insert into person(firstname, lastname) values('Lars', 'Gordon')");
    query.exec_("insert into person(firstname, lastname) values('Roberto', 'Robitaille')");
    query.exec_("insert into person(firstname, lastname) values('Maria', 'Papadopoulos')");

    return True

class Widget(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        lv = QListView(self)
        self.setCentralWidget(lv)
        model = TableModel(self)
        model.setTable("person")
        model.select()
        lv.setModel(model)
        lv.setModelColumn(1)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    if not createConnection():
        sys.exit(-1)
    ex = Widget()
    ex.show()
    sys.exit(app.exec_())