Détecter un clic de souris sur un QGraphicsView
Bonjour,
dans la série "J'apprends à utiliser PyQt4, et j'en profite pour faire un tutoriel (qui sera bientôt en ligne)" je voudrais savoir comment détecter les évènements suivants (mon application possède deux GraphicsView nommés graphicsView_1 et graphicsView_2 respectivement) :
- Détecter la souris lorsqu'elle est sur l'un des deux GraphicsView, tout en sachant sur lequel des deux elle est.
- Détecter de façon analogue un clic gauche simple, un double-clic gauche.
- Détecter de façon analogue un clic droit.
Par avance merci.
Code de l'application :
Code:
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
| # -*- coding: utf-8 -*-
#!/usr/bin/env python
# PRESENTATION : ce script montre comment manipuler du texte entré dans un LineEdit.
# AUTEUR : BAL Christophe
# MAIL : projetmbc@club.fr
# SITE : http://christophe_bal.club.fr/index.php
# DATE DE CREATION : 04/08/2008
#
# TEST(S) EFFECTUE(S) : programme testé sous Windows XP avec succès.
# On importe les bibliothèques que nous allons utiliser.
import sys
from PyQt4 import QtCore, QtGui
# On importe notre application et les boîtes de dialogue.
from window_TestSouris import Ui_window_TestSouris
# # # # # # # # # # # # # # # # # # # # # #
# Comportement de la boîte de dialogue. DEBUT
class window_TestSouris(QtGui.QMainWindow, Ui_window_TestSouris):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_window_TestSouris.__init__(self)
self.setupUi(self)
# On change le texte de la barre de statut.
self.statusbar.showMessage(u"En attente...", 0)
# Comportement de la boîte de dialogue. FIN
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# Lancement de l'application.
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
TestSouris = window_TestSouris()
TestSouris.show()
sys.exit(app.exec_()) |
Code de la fenêtre :
Code:
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
| # -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Documents and Settings\Christophe\Mes documents\2,pyBaNaMa\DebuterAvecPythonEtPyQT\CodesProjets\05-Proj5_DessinGraphSouris\01-TestSouris\window_TestSouris.ui '
#
# Created: Wed Aug 13 18:14:40 2008
# by: PyQt4 UI code generator 4.4.2
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_window_TestSouris(object):
def setupUi(self, window_TestSouris):
window_TestSouris.setObjectName("window_TestSouris")
window_TestSouris.resize(697,538)
window_TestSouris.setMinimumSize(QtCore.QSize(697,538))
window_TestSouris.setMaximumSize(QtCore.QSize(697,538))
self.centralwidget = QtGui.QWidget(window_TestSouris)
self.centralwidget.setGeometry(QtCore.QRect(0,21,697,498))
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.graphicsView_1 = QtGui.QGraphicsView(self.centralwidget)
self.graphicsView_1.setObjectName("graphicsView_1")
self.horizontalLayout.addWidget(self.graphicsView_1)
spacerItem = QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.graphicsView_2 = QtGui.QGraphicsView(self.centralwidget)
self.graphicsView_2.setObjectName("graphicsView_2")
self.horizontalLayout.addWidget(self.graphicsView_2)
window_TestSouris.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(window_TestSouris)
self.menubar.setGeometry(QtCore.QRect(0,0,697,21))
self.menubar.setObjectName("menubar")
window_TestSouris.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(window_TestSouris)
self.statusbar.setGeometry(QtCore.QRect(0,519,697,19))
self.statusbar.setObjectName("statusbar")
window_TestSouris.setStatusBar(self.statusbar)
self.retranslateUi(window_TestSouris)
QtCore.QMetaObject.connectSlotsByName(window_TestSouris)
def retranslateUi(self, window_TestSouris):
window_TestSouris.setWindowTitle(QtGui.QApplication.translate("window_TestSouris", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
window_TestSouris = QtGui.QMainWindow()
ui = Ui_window_TestSouris()
ui.setupUi(window_TestSouris)
window_TestSouris.show()
sys.exit(app.exec_()) |