如何QSqlRelationalTableModel
防止编辑表格的列?例如,有两个表格category
,equipment
如何禁用id
表格category
和Category
表格中列的编辑equipment
?
最小的例子:
import sys
from PyQt5 import QtSql
from PyQt5.Qt import *
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.createConnection()
self.fillTable() # !!!
self.createModel()
self.initUI()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.table_box = QComboBox()
self.table_box.addItems(["Оборудование", "Категории"])
self.table_box.activated[str].connect(self.table_select)
layout = QVBoxLayout(self.centralWidget)
layout.addWidget(self.view)
layout.addWidget(self.table_box)
def createConnection(self):
self.db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName("test_1318914.db") # !!! .db
if not self.db.open():
print("Cannot establish a database connection")
return False
def fillTable(self):
self.db.transaction()
q = QtSql.QSqlQuery()
# vvvvvvvv
q.exec_("DROP TABLE IF EXISTS category;")
q.exec_("CREATE TABLE category (id INT PRIMARY KEY, catname TEXT);")
q.exec_("INSERT INTO category VALUES (1, 'Расходники');")
q.exec_("INSERT INTO category VALUES (2, 'Носители');")
# vvvv
q.exec_("DROP TABLE IF EXISTS equipment;")
q.exec_("CREATE TABLE equipment (Name TEXT, Quantity INT, Category INT);")
q.exec_("INSERT INTO equipment VALUES ('Барабан для принтера', 8, 1);")
q.exec_("INSERT INTO equipment VALUES ('Бумага для принтера', 3, 1);")
q.exec_("INSERT INTO equipment VALUES ('Дискета', 10, 2);")
self.db.commit()
def createModel(self):
self.model = QtSql.QSqlRelationalTableModel()
self.model.setTable("equipment")
self.model.select()
def initUI(self):
self.view = QTableView()
self.view.setModel(self.model)
self.view.setColumnWidth(0, 150)
mode = QAbstractItemView.SingleSelection
self.view.setSelectionMode(mode)
def table_select(self, text):
if text == 'Категории':
self.model.setTable("category")
self.model.select()
elif text == 'Оборудование':
self.model.setTable("equipment")
self.model.select()
def closeEvent(self, event):
if (self.db.open()):
self.db.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Example()
w.setWindowTitle("No edit")
w.resize(430, 250)
w.show()
我意识到我需要创建自己的继承类QSqlRelationalTableModel
并覆盖该方法flags
......我试图重做 Qt 示例,但它不起作用 =( 所有表都变得不可编辑。
1 个回答