Zdarzenia w PyQt
8 December 2008
Comments
PyQt oprócz sygnałów i slotów wykorzystuje mechanizm zdarzeń (events) do "informowania" o akcjach jakie zachodzą w aplikacji. Przykładowo widżet listy "QListWidget" posiada sygnał "itemClicked" informujący o kliknięciu w dany element, lecz zostanie on wysłany dla kliknięcia dowolnym przyciskiem myszy. Dla dokładniejszej kontroli tego typu widżetów stosuje się zdarzenia, które dają także informacje o typie zdarzenia. Przykładowo wykorzystamy:
mouseReleaseEvent (self, QMouseEvent e) mousePressEvent (self, QMouseEvent e)
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
class FileManagerWidget(QtGui.QListWidget):
def __init__(self, parent=None):
"""
QListWidget with handling of mouse events - left and right clicks
"""
super(FileManagerWidget, self).__init__(parent)
# configure the items list
self.setViewMode(QtGui.QListView.IconMode)
self.setLayoutMode(QtGui.QListView.SinglePass)
self.setResizeMode(QtGui.QListView.Adjust)
self.setGridSize(QtCore.QSize(75, 75))
def mouseReleaseEvent(self, event):
"""
mouse button release event
"""
button = event.button()
# select an item on which we clicked
item = self.itemAt(event.x(), event.y())
if item:
self.setCurrentItem(item)
if button == 1:
print 'SIMPLE LEFT CLICK'
def mousePressEvent(self, event):
"""
mouse clicks events
"""
button = event.button()
item = self.itemAt(event.x(), event.y())
if item:
# select the item we clicked
self.setCurrentItem(item)
if button == 1:
print 'LEFT CLICK - DRAG'
if button == 2:
print 'RIGHT CLICK'
app = QtGui.QApplication(sys.argv)
f = FileManagerWidget()
for i in range(1,20):
f.addItem(unicode(i))
f.show()
sys.exit(app.exec_())
RkBlog
Comment article