ฉันมีตารางที่ปรากฏบน GUI ของฉัน ผู้ใช้สามารถแก้ไขตารางนี้ได้จาก GUI ฉันจะรับข้อมูลทั้งหมดที่ได้รับการแก้ไขและอัพเดตในฐานข้อมูลได้อย่างไร? ผู้ใช้ทำเครื่องหมายในช่องสำหรับแต่ละแถวที่ต้องการอัปเดตเป็นฐานข้อมูล ดังนั้นฉันจึงมีรายการแถวทั้งหมดที่ต้องอัปเดต ฉันต้องการรายการสิ่งอันดับ โดยที่แต่ละสิ่งอันดับคือแถวของค่าใหม่ที่ต้องอัปเดต โดยที่ฟิลด์ ID ยังคงไม่เปลี่ยนแปลง (ฉันต้องการทราบวิธีทำให้ผู้ใช้ไม่สามารถแก้ไขบางฟิลด์ได้ด้วย)
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"""
โดยพื้นฐานแล้ว ฉันจำเป็นต้องรู้วิธีรับการเปลี่ยนแปลงใน QTableWidget ใน GUI โดยให้รายการดัชนีที่มีการเปลี่ยนแปลง และวิธีใดวิธีหนึ่งให้อัปเดตการเปลี่ยนแปลงเหล่านั้นลงในฐานข้อมูล นอกจากนี้ การทราบวิธีหยุดผู้ใช้ไม่ให้แก้ไขบางฟิลด์จะเป็นประโยชน์เช่นกัน เนื่องจากจะทำให้ฐานข้อมูลเสียหาย
QtSql
ซึ่งคุณสามารถกำหนดการเชื่อมต่อฐานข้อมูลQtSql.QSqlDatabase
จากนั้นจะถูกจัดการโดยโมเดลฐานข้อมูลQtSql.QSqlTableModel
และเชื่อมต่อกับQtGui.QTableView
ด้วยวิธีนี้ การเปลี่ยนแปลงใน QTableView จะสะท้อนให้เห็นโดยตรงในฐานข้อมูลโดยไม่จำเป็นต้องเขียนการสืบค้นฐานข้อมูลใดๆ จุดเริ่มต้นที่ดีอาจเป็น tutorialspoint.com/pyqt/pyqt_database_handling.htm บทช่วยสอนนี้ - person ImportanceOfBeingErnest   schedule 28.09.2016