我想添加上下文菜单左键点击鼠标按钮按在emailbtn
from PySide import QtCore,QtGui
import sys
class ToolBarUI(QtGui.QWidget):
def __init__(self,*args,**kwargs):
super(ToolBarUI,self).__init__(*args,**kwargs)
self.createActions()
self.floatingToolBar()
pass
def sizeHint(self):
return QtCore.QSize(65,45)
def buttons(self):
x,y=15,35
self.btnVLay=QtGui.QVBoxLayout(self)
self.setLayout(self.btnVLay)
self.btnVLay.setContentsMargins(0,0,0,0)
self.incSavbtn=QtGui.QPushButton("Save")
self.incSavbtn.setMinimumSize(QtCore.QSize(x,y))
self.emailbtn=QtGui.QPushButton("Email")
self.emailbtn.setMinimumSize(QtCore.QSize(x,y))
self.upldbtn=QtGui.QPushButton("Upload")
self.upldbtn.setMinimumSize(QtCore.QSize(x,y))
self.setPrjbtn=QtGui.QPushButton("Set Project")
self.setPrjbtn.setMinimumSize(QtCore.QSize(x,y))
self.setThumb=QtGui.QPushButton("Set thumb")
self.setThumb.setMinimumSize(QtCore.QSize(x,y))
self.shwMatbtn=QtGui.QPushButton("Show Material")
self.shwMatbtn.setMinimumSize(QtCore.QSize(x,y))
self.fixtexbtn=QtGui.QPushButton("Fix Texture Paths")
self.fixtexbtn.setMinimumSize(QtCore.QSize(x,y))
btns = [self.incSavbtn,self.emailbtn,self.upldbtn,self.setPrjbtn,self.setPrjbtn,self.setThumb,self.shwMatbtn,self.fixtexbtn]
[self.btnVLay.addWidget(each) for each in btns]
def contextMenuEvent(self, event):
menu = QtGui.QMenu(self)
menu.addAction(self.emlSel)
menu.addAction(self.emlScn)
menu.addAction(self.emlBufr)
#menu.exec_(self.emailbtn.mapToGlobal(QtCore.QPoint(0,0)))
#menu.exec_(event.globalPos())
def createActions(self):
self.emlSel = QtGui.QAction("Email Selected", self)
self.emlScn = QtGui.QAction("Email this Scene", self)
self.emlBufr = QtGui.QAction("Email Current Frame Buffer", self)
def floatingToolBar(self):
self.buttons()
self.setLayout(self.btnVLay)
self.show()
pass
if __name__ =='__main__':
app = QtGui.QApplication(sys.argv)
win = ToolBarUI()
win.show()
sys.exit(app.exec_())
我已经尝试了contextMenuEvent
方法,但这给了我右键单击:/我错过了什么?
可以在Linux中使用,但不能在windows中使用。
下面是我在windows中使用左键支持的systemtrayicon的代码。这可能也适用于您。
QtCore.QObject.connect(self, QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), self.iconActivated)
def iconActivated(self, reason):
if reason == QtGui.QSystemTrayIcon.Trigger: // left click, right click is Context
self.contextMenu().activateWindow() // menu will disappear on clicking any where other than menu
self.contextMenu().popup(QtGui.QCursor.pos()) // display menu at cursor location
看起来你可以使用setMenu
方法:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtGui
class myWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.actionHello = QtGui.QAction(self)
self.actionHello.setText("Hello")
self.menu = QtGui.QMenu(self)
self.menu.addAction(self.actionHello)
self.buttonShow = QtGui.QPushButton(self)
self.buttonShow.setText("Button with menu")
self.buttonShow.setMenu(self.menu)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.buttonShow)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())