2

右键单击 显示QMenu问题:如何修改此代码以在左键单击时也显示菜单?QLineEdit

在此处输入图像描述

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

    def actionFunct(self, argBool):
        print 'actionFunct()', argBool

    def buildGUI(self):
        self.line=QLineEdit(self)
        self.line.setText('My Line Edit')      

        self.menu=QMenu(self.line)

        self.line.installEventFilter(self)
        self.menu.installEventFilter(self)

        for i in range(3):
            actn=QAction('Action 0%s'%i, self.menu, checkable=True)
            actn.triggered.connect(self.actionFunct)
            self.menu.addAction(actn)

        self.line.setContextMenuPolicy(Qt.CustomContextMenu)
        self.line.connect(self.line, SIGNAL("customContextMenuRequested(QPoint)" ), self.lineClicked)

        layout=QVBoxLayout(self)
        layout.addWidget(self.line)
        self.setLayout(layout)

    def lineClicked(self, QPos):
        print 'lineClicked', QPos
        parentPosition = self.line.mapToGlobal(QPoint(0, 0))        
        menuPosition = parentPosition + QPos

        self.menu.move(menuPosition)
        self.menu.show() 

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.buildGUI()
    w.show()
    sys.exit(app.exec_())
4

2 回答 2

5

SOLUTION # 1 (Thanks to Brendan Abel).

Use installEventFilter() method to route all the lineedit events via easy-to-be-customized eventFilter() method:

self.line.installEventFilter(self)

Now all the events self.line triggers are going to go through eventFilter. There using received event object we query postion using:

event.pos()

which we send to leftClicked() methods as an argument (the same method is called on lineeidit's right-click). from PyQt4.QtCore import * from PyQt4.QtGui import * import sys

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

    def actionFunct(self, argBool):
        print 'actionFunct()', argBool

    def buildGUI(self):
        self.line=QLineEdit(self)
        self.line.setText('My Line Edit')      
        self.line.installEventFilter(self)

        self.menu=QMenu(self.line)

        for i in range(3):
            actn=QAction('Action 0%s'%i, self.menu, checkable=True)
            actn.triggered.connect(self.actionFunct)
            self.menu.addAction(actn)

        self.line.setContextMenuPolicy(Qt.CustomContextMenu)
        self.line.connect(self.line, SIGNAL("customContextMenuRequested(QPoint)" ), self.leftClicked)

        self.line.cursorPositionChanged.connect(self.leftClicked)

        layout=QVBoxLayout(self)
        layout.addWidget(self.line)
        self.setLayout(layout)


    def eventFilter(self, widget, event):
        print 'eventFilter', widget, event
        if widget == self.line and isinstance(event, QMouseEvent) and event.buttons() & Qt.LeftButton:
            self.leftClicked(event.pos())
            return True
        return False

    def leftClicked(self, QPos):
        print 'leftClicked', QPos
        parentPosition = self.line.mapToGlobal(QPoint(0, 0))        
        menuPosition = parentPosition + QPos

        self.menu.move(menuPosition)
        self.menu.show() 

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.buildGUI()
    w.show()
    sys.exit(app.exec_())

SOLUTION # 2

First connect QLineEdit's easiest-to-trigger cursorPositionChanged signal to a method. When on a left-click this method is called query the current mouse cursor position with Qt's QCursor.pos():

current_mouse_cursor=QCursor.pos()

which returns something like:

QtCore.QPoint(852, 595)

Finally move the menu to a queried mouse cursor position and show it:

    self.menu.move(current_mouse_cursor)
    self.menu.show() 

enter image description here

A working code is posted below:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

    def actionFunct(self, argBool):
        print 'actionFunct()', argBool

    def buildGUI(self):
        self.line=QLineEdit(self)
        self.line.setText('My Line Edit')      

        self.menu=QMenu(self.line)

        for i in range(3):

            actn=QAction('Action 0%s'%i, self.menu, checkable=True)
            actn.triggered.connect(self.actionFunct)
            self.menu.addAction(actn)

        self.line.setContextMenuPolicy(Qt.CustomContextMenu)
        self.line.connect(self.line, SIGNAL("customContextMenuRequested(QPoint)" ), self.rightClicked)

        self.line.cursorPositionChanged.connect(self.leftClicked)

        layout=QVBoxLayout(self)
        layout.addWidget(self.line)
        self.setLayout(layout)


    def leftClicked(self, arg):
        print 'leftClicked', arg, QCursor.pos()
        self.menu.move(QCursor.pos())
        self.menu.show() 

    def rightClicked(self, QPos):
        print 'rightClicked', QPos
        parentPosition = self.line.mapToGlobal(QPoint(0, 0))        
        menuPosition = parentPosition + QPos

        self.menu.move(menuPosition)
        self.menu.show() 

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.buildGUI()
    w.show()
    sys.exit(app.exec_())
于 2015-01-20T23:34:06.580 回答
4

您需要在 Window 类上定义一个 eventFilter 方法来过滤/处理事件。

def eventFilter(self, obj, event):
    if obj == self.line and isinstance(event, QMouseEvent) and event.buttons() & Qt.LeftButton:
        self.lineClicked(event.pos())
        return True
    return False
于 2015-01-20T20:54:01.927 回答