-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPushButtonDelegateQt.py
67 lines (57 loc) · 2.87 KB
/
PushButtonDelegateQt.py
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
""" PushButtonDelegateQt.py: Delegate for a clickable button in a model view.
Calls the model's setData() method when clicked, wherein the button clicked action should be handled.
"""
try:
from PyQt5.QtCore import Qt, QEvent, QT_VERSION_STR
from PyQt5.QtWidgets import QStyledItemDelegate, QStyleOptionButton, QStyle, QApplication
except ImportError:
try:
from PyQt4.QtCore import Qt, QEvent, QT_VERSION_STR
from PyQt4.QtGui import QStyledItemDelegate, QStyleOptionButton, QStyle, QApplication
except ImportError:
raise ImportError("PushButtonDelegateQt: Requires PyQt5 or PyQt4.")
__author__ = "Marcel Goldschen-Ohm <[email protected]>"
class PushButtonDelegateQt(QStyledItemDelegate):
""" Delegate for a clickable button in a model view.
Calls the model's setData() method when clicked, wherein the button clicked action should be handled.
"""
def __init__(self, text="", parent=None):
QStyledItemDelegate.__init__(self, parent)
self.text = text
self._isMousePressed = False
def createEditor(self, parent, option, index):
""" Important, otherwise an editor is created if the user clicks in this cell.
"""
return None
def paint(self, painter, option, index):
""" Draw button in cell.
"""
opts = QStyleOptionButton()
opts.state |= QStyle.State_Active
opts.state |= QStyle.State_Enabled
if QT_VERSION_STR[0] == '4':
opts.state |= (QStyle.State_Sunken if self._isMousePressed else QStyle.State_Raised)
elif QT_VERSION_STR[0] == '5':
# When raised in PyQt5, white text cannot be seen on white background.
# Should probably fix this by initializing form styled button, but for now I'll just sink it all the time.
opts.state |= QStyle.State_Sunken
opts.rect = option.rect
opts.text = self.text
QApplication.style().drawControl(QStyle.CE_PushButton, opts, painter)
def editorEvent(self, event, model, option, index):
""" Handle mouse events in cell.
On left button release in this cell, call model's setData() method,
wherein the button clicked action should be handled.
Currently, the value supplied to setData() is the button text, but this is arbitrary.
"""
if event.button() == Qt.LeftButton:
if event.type() == QEvent.MouseButtonPress:
if option.rect.contains(event.pos()):
self._isMousePressed = True
return True
elif event.type() == QEvent.MouseButtonRelease:
self._isMousePressed = False
if option.rect.contains(event.pos()):
model.setData(index, self.text, Qt.EditRole) # Model should handle button click action in its setData() method.
return True
return False