2013-01-10 73 views
2

我想上下文菜單添加到左鍵單擊鼠標按鈕按下的超過emailbtn添加上下文QMenu左點擊一個按鈕在PySide

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方法都試過,但是,讓我在正確的點擊:/ 我錯過了什麼?任何幫助將不勝感激。

回答

1

看起來你可以使用​​方法:

#!/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_()) 
+0

截至目前這有幫助,但我想用左鍵單擊按鈕.. –

+0

我想你的意思是*右鍵單擊?請更明確一點,我似乎不明白你的問題在哪裏 – 2013-01-10 11:07:15

+0

@XJacobs:沒有左鍵點擊按鈕,就像我在這篇文章的第一行中提到的那樣:並且對於'emailbtn' –

4

它可以在Linux,但沒有窗戶。

下面是代碼我用於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 
+0

它確實棒!非常感謝。 – 2013-04-23 16:36:15

+0

.activateWindow()做了訣竅。花了一整天的時間找到這種修復方法,讓我點擊其他地方時左鍵單擊菜單「消失」。謝謝一堆! – panofish