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
| #!/usr/bin/env python3
# coding: utf-8
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
import sys
# L'objet pour gérer mon appli
class myAppli(QApplication):
# Constructeur
def __init__(self, rows, *args, **kwargs):
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# Le widget
self.__widget=myWidget(rows=rows)
# __init__()
# Lancement boucle de traitement des évènements Qt
def exec(self):
# Affichage du widget
self.__widget.show()
# Appel méthode objet hérité
return super().exec()
# exec()
# class myAppli
# La fenêtre principale de mon application
class myWidget(QTableWidget):
# Constructeur
def __init__(self, rows, *args, **kwargs):
# Appel méthode objet hérité
super().__init__(rows, 3, *args, **kwargs)
# Le mappeur de signal
mapBox=QSignalMapper(parent=self)
# L'info que le mappeur transmettra sera un objet Qt
mapBox.mappedObject.connect(self.__slotBox)
# Remplissage
for i in range(rows):
# Première colonne: Un texte
self.setCellWidget(i, 0, QLabel("Hello %d" % i, parent=self))
# Seconde colonne: Une saisie
self.setCellWidget(i, 1, QLineEdit(parent=self))
# Troisième colonne: Une case à cocher
b=QCheckBox(parent=self)
b.setProperty("data", QVariant((i, 3))) # Info interne CheckBox
b.stateChanged[int].connect(mapBox.map) # Le clic CheckBox est envoyé au mappeur
mapBox.setMapping(b, b) # Le mappeur associe le CheckBox au signal qu'il renvoie
w=QWidget(parent=self)
l=QHBoxLayout(w)
l.setContentsMargins(0, 0, 0, 0)
l.setSpacing(0)
l.setAlignment(Qt.AlignmentFlag.AlignCenter)
l.addWidget(b)
self.setCellWidget(i, 2, w)
# for
self.resize(400, 380)
# __init()
def __slotBox(self, w):
p=w.property("data")
print("value=%s, pos=%d, %d" % (w.checkState(), p[0], p[1]))
# __slotBox
# class myWidget
# Le programme principal
if __name__ == "__main__":
# L'application principale Qt
sys.exit(myAppli(10, sys.argv).exec())
# if |
Partager