1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import sys, sqlite3
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget,
QHBoxLayout,QTableView)
from PyQt5.QtSql import (QSqlDatabase, QSqlQuery, QSqlTableModel,
QSqlRelation,QSqlRelationalTableModel)
from PyQt5.QtCore import QModelIndex
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
db=self.connectdb()
widget=QWidget(self)
self.setCentralWidget(widget)
layout=QHBoxLayout()
self.tableViewEmployes=QTableView(self)
self.tableViewformasuivi=QTableView(self)
layout.addWidget(self.tableViewEmployes)
layout.addWidget(self.tableViewformasuivi)
widget.setLayout(layout)
self.modellisteemloyes=QSqlTableModel()
self.modellisteemloyes.setTable("Agents")
self.modellisteemloyes.select()
self.tableViewEmployes.setModel(self.modellisteemloyes)
self.modellisteformation=QSqlRelationalTableModel()
self.modellisteformation.setTable("Formations")
self.modellisteformation.setRelation(1,QSqlRelation('Listeformations', 'FormId', 'intitule'))
self.modellisteformation.select()
self.tableViewformasuivi.setModel(self.modellisteformation)
self.tableViewEmployes.clicked.connect(self.setFilter)
def setFilter(self, ind):
matricule=self.modellisteemloyes.data(self.modellisteemloyes.index(ind.row(), 0, QModelIndex()))
self.modellisteformation.setFilter("Matricule = {}".format(matricule))
def connectdb(self):
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(":memory:")
if not db.open():
return False
Agents = [(1, "Agent 1"),
(2, "agent 2"),
(3,"Agent 3")]
Listeformations=[(1, "Formation 1"),
(2, "Formation 2"),
(3, "Formation 3"),
(4, "Formation 4")]
#liste des formations suivies (Matricule, formation)
Formations=[
(1,1),
(1,2),
(2,2)]
query=QSqlQuery()
query.exec_("create table Agents(Matricule int, Nom varchar(20))")
query.exec_("create table Formations(Matricule int, FormId)")
query.exec_("create table Listeformations(FormId, Intitule varchar(20))")
for value in Agents:
query.exec_("insert into Agents (Matricule, Nom) values {}".format(value))
for value in Formations:
query.exec_("insert into Formations(Matricule, FormId) values {}".format(value))
for value in Listeformations:
query.exec_("insert into Listeformations(FormId, Intitule) values {}".format(value))
return True
if __name__ == "__main__":
application = QApplication(sys.argv)
win = MyWindow()
win.show()
con = sqlite3.connect(":memory:")
con.close()
sys.exit(application.exec_()) |
Partager