-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxsiMenuButton.py
37 lines (29 loc) · 1.23 KB
/
xsiMenuButton.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
from Qt import QtGui, QtCore, QtWidgets
from Qt.QtCore import *
from Qt.QtGui import *
class TooglableMenuButton (QtWidgets.QPushButton):
def __init__ (self, path, menu, parent=None):
QtWidgets.QPushButton.__init__(self, parent)
self.pixmap = QPixmap(path)
self.menu = menu
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.menuButton_onClicked)
def paintEvent (self, event):
#Draw the corner arrow
#
QtWidgets.QPushButton.paintEvent(self, event)
style = self.style()
opt = QtWidgets.QStyleOptionButton()
self.initStyleOption(opt)
p = QtGui.QPainter(self)
if not self.pixmap.isNull():
style.drawItemPixmap(p, self.rect(), Qt.AlignLeft|Qt.AlignTop, self.pixmap)
def menuButton_onClicked(self, point):
# set the menu position on the button's left side
sender = self.sender()
globalPos = sender.mapToGlobal(QPoint(0 , 0))
pos = QPoint(globalPos.x() - self.menu.sizeHint().width(), globalPos.y())
self.menu.exec_(pos)
def setMenu(self, menu):
QtWidgets.QPushButton.setMenu(self, menu)
menu.installEventFilter(self)