2

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

4

2 回答 2

4

它适用于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 
于 2013-01-25T09:48:04.493 回答
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_())
于 2013-01-10T10:40:43.097 回答