我正在使用 QListWidget、QSqlTableModel、QtableView 和 QDataWidgetMapper。QListWidget 是 ItemIsUserCheckable。我能够将 QListWidget 中的选定项目以字符串格式保存到单个字段中的 QtSql 表中(将列表项转换为字符串为“Green,Green Grapes,Oranges”。但同时我想更改选定的项目并更新来自字符串数据的 QListWidget 的项目。怎么办。是否可能。或者请让我知道是否有其他解决方案。下面是示例代码:
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(391, 496)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(9, 9, 373, 188))
self.listWidget.setObjectName("listWidget")
self.save_btn = QtWidgets.QPushButton(self.centralwidget)
self.save_btn.setGeometry(QtCore.QRect(100, 230, 75, 23))
self.save_btn.setObjectName("save_btn")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(9, 203, 27, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(56, 203, 181, 20))
self.lineEdit.setObjectName("lineEdit")
self.update_btn = QtWidgets.QPushButton(self.centralwidget)
self.update_btn.setGeometry(QtCore.QRect(293, 229, 75, 23))
self.update_btn.setObjectName("update_btn")
self.edit_btn = QtWidgets.QPushButton(self.centralwidget)
self.edit_btn.setGeometry(QtCore.QRect(199, 229, 75, 23))
self.edit_btn.setObjectName("edit_btn")
self.tableView = QtWidgets.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(9, 258, 373, 188))
self.tableView.setObjectName("tableView")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 391, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("fruites.db")
if db.open():
if db.open():
query = QSqlQuery()
query.exec_("""CREATE TABLE IF NOT EXISTS store(name TEXT, items TEXT)""")
self.store_model = QtSql.QSqlTableModel()
self.store_model.setTable("store")
self.store_model.select()
self.tableView.setModel(self.store_model)
list_ = ["Green", "Green Apple","Green Grapes", "Bananas", "Oranges"]
list_ = dict.fromkeys(list_, 0)
for i in list_:
item = QtWidgets.QListWidgetItem(i)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)
self.listWidget.addItem(item)
self.save_btn.clicked.connect(self.save_item)
self.edit_btn.clicked.connect(self.edit_item)
self.update_btn.clicked.connect(self.submit_item)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.save_btn.setText(_translate("MainWindow", "Save"))
self.label.setText(_translate("MainWindow", "Name"))
self.update_btn.setText(_translate("MainWindow", "Update"))
self.edit_btn.setText(_translate("MainWindow", "Edit"))
def save_item(self):
name = self.lineEdit.text()
strng = ''
checked = []
for row in range(self.listWidget.count()):
item = self.listWidget.item(row)
if item.checkState():
checked.append(item)
strng = ", " .join(i.text() for i in checked)
r = self.store_model.record()
r.setValue("name", name)
r.setValue("items", strng)
self.store_model.insertRecord(-1, r)
self.store_model.select()
self.lineEdit.clear()
for i in range(self.listWidget.count()):
item = self.listWidget.item(i)
item.setCheckState(QtCore.Qt.Unchecked)
def edit_item(self):
selected = self.tableView.selectedIndexes()
if not self.store_model.rowCount():
return
index = self.tableView.currentIndex()
if index.isValid():
row = index.row()
else:
row = 0
self.store_mapper = QtWidgets.QDataWidgetMapper()
self.store_mapper.setSubmitPolicy(QtWidgets.QDataWidgetMapper.ManualSubmit)
self.store_mapper.setModel(self.tableView.model())
self.store_mapper.addMapping(self.lineEdit, 0)
self.store_mapper.addMapping(self.listWidget, 1)
self.store_mapper.setCurrentIndex(row)
def submit_item(self):
self.store_mapper.submit()
self.lineEdit.clear()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())