添加上下文QMenu以左键单击PySide中的按钮

时间:2013-01-10 07:03:53

标签: contextmenu pyside qmenu

我想将上下文菜单添加到按下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方法,但右键点击了我:/ 我错过了什么?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

它适用于Linux但不适用于Windows。

下面是我在systemtrayicon中使用的代码,在windows中有左键单击支持。这对你也有用。

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 

答案 1 :(得分:1)

看起来您可以使用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_())