4.3 支持表間關(guān)系的模型
QSqlRelationalTableModel類用于表間關(guān)系的模型,其繼承層次如下:
QObject - QAbstractltemModel - QAbstractTableModel - QSqlQueryModel -QSqlTableModel - QSqlRelationalTableModel
構(gòu)造函數(shù)為:
QSqlRelationalQueryModel([QObject parent=None][, ][QSqlDatabase db = QSqlDatabase() ])
QSqlRelationalQueryModel類新增了以下方法(詳見http://doc.qt.io/qt-5/qsqlrelationaltablemodel.html):
setRelation(int column, QSqlRelation relation):為column字段設(shè)置鏈接唆鸡。 鏈連接的信息由QSqiReiation實(shí)例來(lái)表示滩援。
setJoinMode(QSqlRelationalTableModel.joinMode joinMode):設(shè)置模型的綁定模式矩欠。joinMode的參數(shù)可為:
InnerJoin,0:默認(rèn)值嘉抓。不顯示沒有外鍵的記錄。
LeftJoin,1:顯示沒有外鍵的記錄抬虽。
QSqlRelation類用于表示數(shù)據(jù)庫(kù)表之間的關(guān)系夏伊,其構(gòu)造函數(shù)為:
QSqlRelation(QString tableName, QString indexColumn, QString displayColumn):tableName為外鍵引用的數(shù)據(jù)庫(kù)表名;indexColumn為外鍵字段刻肄;displayColumn為顯示的字段瓤球。QSqlRelation類有若干方法,但使用不頻繁(詳見http://doc.qt.io/qt-5/qsqlrelation.html)敏弃。
下表為data.sqlite數(shù)據(jù)庫(kù)使用關(guān)系模型的代碼:
from PyQt5 import QtCore, QtWidgets, QtSql
import sys
def addRecord():
stm.insertRow(stm.rowCount())
def delRecord():
stm.removeRow(tv.currentIndex().row())
stm.select()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("QRelationalSqlTableModel")
con = QtSql.QSqlDatabase.addDatabase('QSQLITE')
con.setDatabaseName('data.sqlite')
con.open()
stm = QtSql.QSqlRelationalTableModel(parent = window)
stm.setTable('good')
stm.setSort(2, QtCore.Qt.AscendingOrder)
將good表中的category字段設(shè)置為到category表的鏈接
stm.setRelation(3, QtSql.QSqlRelation('category', 'id', 'catname'))
stm.select()
stm.setHeaderData(1, QtCore.Qt.Horizontal, '名稱')
stm.setHeaderData(2, QtCore.Qt.Horizontal, '數(shù)量')
stm.setHeaderData(3, QtCore.Qt.Horizontal, '類別')
vbox = QtWidgets.QVBoxLayout()
tv = QtWidgets.QTableView()
tv.setModel(stm)
tv.hideColumn(0)
tv.setColumnWidth(1, 150)
tv.setColumnWidth(2, 60)
tv.setColumnWidth(3, 150)
vbox.addWidget(tv)
btnAdd = QtWidgets.QPushButton("添加記錄(&A)")
btnAdd.clicked.connect(addRecord)
vbox.addWidget(btnAdd)
btnDel = QtWidgets.QPushButton("刪除記錄(&A)")
btnDel.clicked.connect(delRecord)
vbox.addWidget(btnDel)
window.setLayout(vbox)
window.resize(430, 250)
window.show()
sys.exit(app.exec_())
運(yùn)行結(jié)果: <wbr> <wbr> 4.3 <wbr>支持表間關(guān)系的模型
<wbr> <wbr> QSqlRelationalTableModel<wbr>類用于表間關(guān)系的模型卦羡,其繼承層次如下:
<wbr> <wbr> QObject - QAbstractltemModel - QAbstractTableModel - QSqlQueryModel -QSqlTableModel - QSqlRelationalTableModel<wbr>
<wbr> <wbr> 構(gòu)造函數(shù)為:
<wbr> <wbr> QSqlRelationalQueryModel<wbr>([QObject parent=None][, ][QSqlDatabase db = QSqlDatabase() ])
<wbr> <wbr> QSqlRelationalQueryModel<wbr>類新增了以下方法(詳見http://doc.qt.io/qt-5/qsqlrelationaltablemodel<wbr>.html):
- setRelation(int column, QSqlRelation relation):為column字段設(shè)置鏈接。 鏈連接的信息由QSqiReiation實(shí)例來(lái)表示麦到。
- setJoinMode(QSqlRelationalTableModel<wbr>.joinMode joinMode):設(shè)置模型的綁定模式绿饵。joinMode的參數(shù)可為:
- InnerJoin,0:默認(rèn)值。不顯示沒有外鍵的記錄隅要。
- LeftJoin,1:顯示沒有外鍵的記錄蝴罪。
<wbr> <wbr> <wbr>QSqlRelation類用于表示數(shù)據(jù)庫(kù)表之間的關(guān)系,其構(gòu)造函數(shù)為:
<wbr> <wbr> QSqlRelation(QString tableName, QString indexColumn, QString displayColumn):tableName為外鍵引用的數(shù)據(jù)庫(kù)表名步清;indexColumn為外鍵字段要门;displayColumn為顯示的字段。QSqlRelation類有若干方法廓啊,但使用不頻繁(詳見http://doc.qt.io/qt-5/qsqlrelation.html)欢搜。
下表為data.sqlite數(shù)據(jù)庫(kù)使用關(guān)系模型的代碼: <wbr>
from PyQt5 import QtCore, QtWidgets, QtSql
import sys
def addRecord():
<wbr> <wbr> stm.insertRow(stm.rowCount())
def delRecord():
<wbr> <wbr> stm.removeRow(tv.currentIndex().row())
<wbr> <wbr> stm.select()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("QRelationalSqlTableModel<wbr>")
con = QtSql.QSqlDatabase.addDatabase('QSQLITE')
con.setDatabaseName('data.sqlite') <wbr>
con.open()
stm = QtSql.QSqlRelationalTableModel<wbr>(parent = window)
stm.setTable('good')
stm.setSort(2, QtCore.Qt.AscendingOrder)
將good表中的category字段設(shè)置為到category表的鏈接
stm.setRelation(3, QtSql.QSqlRelation('category', 'id', 'catname'))
stm.select()
stm.setHeaderData(1, QtCore.Qt.Horizontal, '名稱')
stm.setHeaderData(2, QtCore.Qt.Horizontal, '數(shù)量')
stm.setHeaderData(3, QtCore.Qt.Horizontal, '類別')
vbox = QtWidgets.QVBoxLayout()
tv = QtWidgets.QTableView()
tv.setModel(stm)
tv.hideColumn(0)
tv.setColumnWidth(1, 150)
tv.setColumnWidth(2, 60)
tv.setColumnWidth(3, 150)
vbox.addWidget(tv)
btnAdd = QtWidgets.QPushButton("添加記錄(&A)")
btnAdd.clicked.connect(addRecord)
vbox.addWidget(btnAdd)
btnDel = QtWidgets.QPushButton("刪除記錄(&A)")
btnDel.clicked.connect(delRecord)
vbox.addWidget(btnDel)
window.setLayout(vbox)
window.resize(430, 250)
window.show()
sys.exit(app.exec_())
運(yùn)行結(jié)果: