如何使用 Python 和 SQLite3 在 PyQT 中的表中获取用户已更改的信息 [英] How do I get the information that the user has changed in a table in PyQT with Python and SQLite3

查看:35
本文介绍了如何使用 Python 和 SQLite3 在 PyQT 中的表中获取用户已更改的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 GUI 上有一个表.用户可以从 GUI 编辑此表.如何获取所有已编辑的信息并在数据库中更新它?用户选中他们想要更新到数据库的每一行的复选框,所以我有一个需要更新的所有行的列表.我想要一个元组列表,其中每个元组都是一行需要更新的新值,因为 ID 字段保持不变(我还想知道如何让用户无法编辑某些字段).

I have a table that comes up on my GUI. The user can edit this table from the GUI. how do I get all of the information that has been edited and update it in the database? The user checks the checkbox for each row they want to have updated to the database, so I have a list of all rows that require updating. I want to have a list of tuples, where each tuple is a row of new values that need to be updated, given that the ID field remains unchanged (I also want to know how to make the user unable to edit some fields).

def click_btn_mailouts(self):
    self.screen_name = "mailouts"
    self.cur.execute("""SELECT s.StudentID, s.FullName, m.PreviouslyMailed, m.nextMail, m.learnersDate, m.RestrictedDate, m.DefensiveDate FROM
                        StudentProfile s LEFT JOIN Mailouts m ON s.studentID=m.studentID""")
    self.all_data = self.cur.fetchall()

    self.table.setRowCount(len(self.all_data))
    self.tableFields = ["Check","Full name","Previously mailed?","Next mail","learnersDate","Restricted date","Defensive driving date"]
    self.table.setColumnCount(len(self.tableFields))
    self.table.setHorizontalHeaderLabels(self.tableFields)
    self.checkbox_list = []
    for i, item in enumerate(self.all_data):
        FullName = QtGui.QTableWidgetItem(str(item[1]))
        PreviouslyMailed = QtGui.QTableWidgetItem(str(item[2]))
        LearnersDate = QtGui.QTableWidgetItem(str(item[3]))
        RestrictedDate = QtGui.QTableWidgetItem(str(item[4]))
        DefensiveDate = QtGui.QTableWidgetItem(str(item[5]))
        NextMail = QtGui.QTableWidgetItem(str(item[6]))
        self.table.setItem(i, 1, FullName)
        self.table.setItem(i, 2, PreviouslyMailed)
        self.table.setItem(i, 3, LearnersDate)
        self.table.setItem(i, 4, RestrictedDate)
        self.table.setItem(i, 5, DefensiveDate)
        self.table.setItem(i, 6, NextMail)
        chkBoxItem = QtGui.QTableWidgetItem()
        chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
        chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
        self.checkbox_list.append(chkBoxItem)
        self.table.setItem(i, 0, self.checkbox_list[i])

"""here is the format that I have for the edit function"""

def click_btn_edit(self):
    checkedRows = []
    for i, checkbox in enumerate(self.checkbox_list):
        if checkbox.checkState() == QtCore.Qt.Checked:
            checkedRows.append(i)
            """as the list itterates, if the checkbox item is ticked, 
            it passes through the if statement, otherwise it is ignored.
            checkedRows becomes a list of all the indexes in the table where
            an edit needs to be made"""                

所以基本上我需要知道如何在给定已进行更改的索引列表的情况下获取 GUI 中 QTableWidget 中所做的更改,并以某种方式将这些更改更新到数据库中.了解如何阻止用户编辑某些字段也会很有帮助,因为这会弄乱数据库.

So basically I need to know how to get the changes made in the QTableWidget in the GUI given a list of indexes where changes have been made, and somehow get those changes updated into the database. It would also be helpful to know how to stop the user from editing some of the fields, as that would mess up the database.

推荐答案

您可以做一些不同的事情.

You can do a few different things.

为了防止编辑,您可以删除不希望用户编辑的项目的编辑标志

To prevent editing, you can just remove the edit flag for the items you don't want the user to edit

FullName.setFlags(FullName.flags() & ~Qt.ItemIsEditable)

看起来您正在存储原始数据(即 self.all_data).您可以将所选表格单元格中的数据与原始数据进行比较,并仅更新已更改的字段.

It looks like you're storing the original data (i.e. self.all_data). You could just compare the data in the selected table cells with the original data and only update fields that have changed.

您还可以连接到 itemChanged 表格小部件的信号,并保留自上次刷新以来所有已更改索引的运行列表

You could also connect to the itemChanged signal for the table widget and keep a running list of all the indexes that have changed since the last refresh

    ...
    self.changed_items = set()
    self.table.itemChanged.connect(self.log_change)

def log_change(self, item):
    self.changed_items.add(item)

或者,根据您想要多少控制,您还可以创建一个 QItemDelegate 来完成所有这些.

Alternatively, depending on how much control you want, you can also create a QItemDelegate to do all of this.

这篇关于如何使用 Python 和 SQLite3 在 PyQT 中的表中获取用户已更改的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆