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
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
# Python 2.7
import sys, os
from PyQt4 import QtCore, QtGui
#############################################################################
class Fenetre(QtGui.QWidget):
def __init__(self, parent=None):
super(Fenetre, self).__init__(parent)
self.bouton1 = QtGui.QPushButton(u"bouton 1", self)
self.bouton1.focusInEvent = self.quelWidgetALeFocus
self.bouton2 = QtGui.QPushButton(u"bouton 2", self)
self.bouton2.focusInEvent = self.quelWidgetALeFocus
self.edit = QtGui.QLineEdit(self)
self.edit.focusInEvent = self.quelWidgetALeFocus
self.combo = QtGui.QComboBox(self)
self.combo.focusInEvent = self.quelWidgetALeFocus
posit = QtGui.QGridLayout()
posit.addWidget(self.bouton1, 0, 0)
posit.addWidget(self.bouton2, 1, 0)
posit.addWidget(self.edit, 2, 0)
posit.addWidget(self.combo, 3, 0)
self.setLayout(posit)
def quelWidgetALeFocus(self, fEvent):
focusW = self.focusWidget()
if focusW==self.bouton1:
print u"le bouton 1 vient d'avoir le focus!"
QtGui.QPushButton.focusInEvent(self.bouton1, fEvent)
elif focusW==self.bouton2:
print u"le bouton 2 vient d'avoir le focus!"
QtGui.QPushButton.focusInEvent(self.bouton2, fEvent)
elif focusW==self.edit:
print u"le qlineedit vient d'avoir le focus!"
QtGui.QLineEdit.focusInEvent(self.edit, fEvent)
elif focusW==self.combo:
print u"le combobox vient d'avoir le focus!"
QtGui.QComboBox.focusInEvent(self.combo, fEvent)
#############################################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
fen = Fenetre()
fen.show()
sys.exit(app.exec_()) |
Partager