Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
nouveaux_widgets [2016/06/02 16:51] tyrtamos |
nouveaux_widgets [2016/06/04 08:49] tyrtamos |
||
---|---|---|---|
Ligne 54: | Ligne 54: | ||
==== Création du fichier " | ==== Création du fichier " | ||
- | On va maintenant créer le fichier " | + | On va maintenant créer le fichier " |
+ | |||
+ | Ainsi, seule la 1ère partie mentionnée " | ||
<code python># | <code python># | ||
Ligne 62: | Ligne 64: | ||
from PyQt5 import QtGui, QtDesigner | from PyQt5 import QtGui, QtDesigner | ||
- | # ===== a adapter selon le widget! ========================================== | + | # ===== adapter selon le widget! ========================================== |
# nom (str) du fichier du widget sans extension | # nom (str) du fichier du widget sans extension | ||
FICHIERWIDGET = " | FICHIERWIDGET = " | ||
- | # nom (str) de la classe du widget | + | # nom (str) de la classe du widget |
NOMCLASSEWIDGET = " | NOMCLASSEWIDGET = " | ||
- | # nom (str) de l' | + | # nom (str) de l' |
NOMWIDGET = " | NOMWIDGET = " | ||
- | # groupe (str) de widgets pour Designer | + | # groupe (str) de widgets pour affichage dans Designer |
GROUPEWIDGET = "Mes widgets perso" | GROUPEWIDGET = "Mes widgets perso" | ||
# texte (str) pour le toolTip dans Designer | # texte (str) pour le toolTip dans Designer | ||
Ligne 75: | Ligne 77: | ||
# texte (str) pour le whatsThis dans Designer | # texte (str) pour le whatsThis dans Designer | ||
TEXTEWHATSTHIS = "Un QLineEdit avec un fond jaune" | TEXTEWHATSTHIS = "Un QLineEdit avec un fond jaune" | ||
- | # icone (rien ou QPixmap) pour presenter le widget | + | # icone (rien ou un fichier image ou un QPixmap) pour afficher |
- | ICONEWIDGET = QtGui.QIcon() | + | ICONEWIDGET = QtGui.QIcon() |
# =========================================================================== | # =========================================================================== | ||
Ligne 167: | Ligne 169: | ||
return FICHIERWIDGET | return FICHIERWIDGET | ||
</ | </ | ||
- | |||
- | Ça parait compliqué comme ça, mais en fait, il n'y a qu'à adapter selon le widget: | ||
- | * les valeurs des variables en majuscules (utilisez les commentaires dans le code), | ||
- | * le nom de la classe, | ||
- | * et, bien sûr, le nom du fichier du widget qui devra se terminer par " | ||
==== Lancement du Designer ==== | ==== Lancement du Designer ==== | ||
Ligne 178: | Ligne 175: | ||
Dans les variables d' | Dans les variables d' | ||
- | |||
* le chemin du répertoire " | * le chemin du répertoire " | ||
* le chemin du répertoire " | * le chemin du répertoire " | ||
+ | |||
En conséquence, | En conséquence, | ||
Ligne 239: | Ligne 236: | ||
</ | </ | ||
- | Quand vous lancez ce code Python, le Designer vient à l' | + | Quand vous lancez ce code Python, le Designer vient à l' |
- | A noter que ce script qui appelle le Designer nécessite de pouvoir retrouver automatiquement les adresses à l' | + | A noter que ce script qui appelle le Designer nécessite de pouvoir retrouver automatiquement les adresses à l' |
+ | <code python> | ||
+ | Prefix = Lib/ | ||
+ | Binaries = . | ||
+ | Libraries = . | ||
+ | Plugins = plugins | ||
+ | Translations = translations | ||
+ | </ | ||
===== Un autre exemple plus complet ===== | ===== Un autre exemple plus complet ===== | ||
Ligne 259: | Ligne 263: | ||
* 2ème ligne: un QLabel affichant " | * 2ème ligne: un QLabel affichant " | ||
- | Pour cet exemple, je me suis inspiré du tutoriel | + | Pour cet exemple, je me suis inspiré du tutoriel traduit en français ici: [[http:// |
Voilà le code définissant ce nouveau widget, qui sera contenu dans le fichier " | Voilà le code définissant ce nouveau widget, qui sera contenu dans le fichier " | ||
Ligne 265: | Ligne 269: | ||
<code python># | <code python># | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
- | # Python v3 | + | # Python v3 PyQt5 |
- | + | ||
- | from PyQt4 import QtCore, | + | from PyQt5 import QtCore, |
+ | |||
############################################################################# | ############################################################################# | ||
- | class GeoLocationWidget(QtGui.QWidget): | + | class GeoLocationWidget(QtWidgets.QWidget): |
""" | """ | ||
""" | """ | ||
+ | | ||
# crée 2 nouveaux signaux du widget, émis à chaque changement des valeurs | # crée 2 nouveaux signaux du widget, émis à chaque changement des valeurs | ||
latitudeChanged = QtCore.pyqtSignal(float) | latitudeChanged = QtCore.pyqtSignal(float) | ||
longitudeChanged = QtCore.pyqtSignal(float) | longitudeChanged = QtCore.pyqtSignal(float) | ||
- | + | | |
- | # | + | |
def __init__(self, | def __init__(self, | ||
- | super(GeoLocationWidget, | + | super().__init__(parent) |
# crée le 1er spinbox: pour la latitude | # crée le 1er spinbox: pour la latitude | ||
- | latitudeLabel = QtGui.QLabel(" | + | latitudeLabel = QtWidgets.QLabel(" |
- | self.latitudeSpinBox = QtGui.QDoubleSpinBox() | + | self.latitudeSpinBox = QtWidgets.QDoubleSpinBox() |
self.latitudeSpinBox.setRange(-90.0, | self.latitudeSpinBox.setRange(-90.0, | ||
self.latitudeSpinBox.setDecimals(12) | self.latitudeSpinBox.setDecimals(12) | ||
- | # le signal valuechanged est redirigée sur le signal global latitudeChanged | + | # le signal valuechanged |
- | self.latitudeSpinBox.valueChanged.connect(GeoLocationWidget.latitudeChanged) | + | self.latitudeSpinBox.valueChanged.connect(self.latitudeChanged) |
# crée le 2e spinbox: pour la longitude | # crée le 2e spinbox: pour la longitude | ||
- | longitudeLabel = QtGui.QLabel(" | + | longitudeLabel = QtWidgets.QLabel(" |
- | self.longitudeSpinBox = QtGui.QDoubleSpinBox() | + | self.longitudeSpinBox = QtWidgets.QDoubleSpinBox() |
self.longitudeSpinBox.setRange(-180.0, | self.longitudeSpinBox.setRange(-180.0, | ||
self.longitudeSpinBox.setDecimals(12) | self.longitudeSpinBox.setDecimals(12) | ||
- | # le signal valuechanged est redirigée sur le signal global longitudeChanged | + | # le signal valuechanged |
- | self.longitudeSpinBox.valueChanged.connect(GeoLocationWidget.longitudeChanged) | + | self.longitudeSpinBox.valueChanged.connect(self.longitudeChanged) |
+ | |||
# positionne les 4 objets graphiques en deux lignes dans le QWidget | # positionne les 4 objets graphiques en deux lignes dans le QWidget | ||
- | layout = QtGui.QGridLayout(self) | + | layout = QtWidgets.QGridLayout(self) |
layout.addWidget(latitudeLabel, | layout.addWidget(latitudeLabel, | ||
layout.addWidget(self.latitudeSpinBox, | layout.addWidget(self.latitudeSpinBox, | ||
Ligne 305: | Ligne 309: | ||
layout.addWidget(self.longitudeSpinBox, | layout.addWidget(self.longitudeSpinBox, | ||
self.setLayout(layout) | self.setLayout(layout) | ||
+ | | ||
# | # | ||
- | # permet au Designer de lire/ | + | # permet au Designer de lire/ |
+ | |||
@QtCore.pyqtSlot() | @QtCore.pyqtSlot() | ||
def getLatitude(self): | def getLatitude(self): | ||
- | """ | + | """ |
+ | | ||
return self.latitudeSpinBox.value() | return self.latitudeSpinBox.value() | ||
@QtCore.pyqtSlot(float) | @QtCore.pyqtSlot(float) | ||
def setLatitude(self, | def setLatitude(self, | ||
- | """ | + | """ |
+ | | ||
if latitude != self.latitudeSpinBox.value(): | if latitude != self.latitudeSpinBox.value(): | ||
self.latitudeSpinBox.setValue(latitude) | self.latitudeSpinBox.setValue(latitude) | ||
- | |||
- | @QtCore.pyqtSlot() | ||
- | def razLatitude(self): | ||
- | """ | ||
- | self.latitudeSpinBox.setValue(0.0) | ||
# définit la propriété pour permettre la configuration par Designer | # définit la propriété pour permettre la configuration par Designer | ||
- | latitude = QtCore.pyqtProperty(float, | + | latitude = QtCore.pyqtProperty(float, |
+ | | ||
# | # | ||
- | # permet au Designer de lire/ | + | # permet au Designer de lire/ |
+ | |||
@QtCore.pyqtSlot() | @QtCore.pyqtSlot() | ||
def getLongitude(self): | def getLongitude(self): | ||
- | """ | + | """ |
+ | | ||
return self.longitudeSpinBox.value() | return self.longitudeSpinBox.value() | ||
+ | |||
@QtCore.pyqtSlot(float) | @QtCore.pyqtSlot(float) | ||
def setLongitude(self, | def setLongitude(self, | ||
- | """ | + | """ |
+ | | ||
if longitude != self.longitudeSpinBox.value(): | if longitude != self.longitudeSpinBox.value(): | ||
self.longitudeSpinBox.setValue(longitude) | self.longitudeSpinBox.setValue(longitude) | ||
- | + | ||
- | @QtCore.pyqtSlot() | + | |
- | def razLongitude(self): | + | |
- | """ | + | |
- | self.longitudeSpinBox.setValue(0.0) | + | |
# définit la propriété pour permettre la configuration par Designer | # définit la propriété pour permettre la configuration par Designer | ||
- | longitude = QtCore.pyqtProperty(float, | + | longitude = QtCore.pyqtProperty(float, |
</ | </ | ||
Ligne 371: | Ligne 369: | ||
# nom (str) du fichier du widget sans extension | # nom (str) du fichier du widget sans extension | ||
FICHIERWIDGET = " | FICHIERWIDGET = " | ||
- | # nom (str) de la classe du widget | + | # nom (str) de la classe du widget |
NOMCLASSEWIDGET = " | NOMCLASSEWIDGET = " | ||
- | # nom (str) de l' | + | # nom (str) de l' |
NOMWIDGET = " | NOMWIDGET = " | ||
- | # groupe (str) de widgets pour Designer | + | # groupe (str) de widgets pour affichage dans Designer |
GROUPEWIDGET = "Mes widgets perso" | GROUPEWIDGET = "Mes widgets perso" | ||
# texte (str) pour le toolTip dans Designer | # texte (str) pour le toolTip dans Designer | ||
Ligne 381: | Ligne 379: | ||
# texte (str) pour le whatsThis dans Designer | # texte (str) pour le whatsThis dans Designer | ||
TEXTEWHATSTHIS = " | TEXTEWHATSTHIS = " | ||
- | # icone (rien ou un QPixmap) pour présenter le widget | + | # icone (rien ou un fichier image ou un QPixmap) pour afficher |
- | ICONEWIDGET = QtGui.QIcon() | + | ICONEWIDGET = QtGui.QIcon() |
... | ... | ||
... | ... | ||
############################################################################# | ############################################################################# | ||
class GeoLocationPlugin(QtDesigner.QPyDesignerCustomWidgetPlugin): | class GeoLocationPlugin(QtDesigner.QPyDesignerCustomWidgetPlugin): | ||
- | """ | ||
- | à renommer selon le widget | ||
- | """ | ||
- | |||
- | # | ||
- | def __init__(self, | ||
- | super(GeoLocationPlugin, | ||
- | self.initialized = False | ||
... | ... | ||
... | ... |