alphanumeric
alphanumeric

Reputation: 19379

How to show QMenu on left click

The QMenu shows up on a QLineEdit right-click. Question: How to modify this code to show the menu on a left-click as well?

enter image description here

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_())

Upvotes: 2

Views: 3478

Answers (2)

Brendan Abel
Brendan Abel

Reputation: 37569

You need to define an eventFilter method on your Window class to filter/handle the event.

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

Upvotes: 4

alphanumeric
alphanumeric

Reputation: 19379

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_())

Upvotes: 5

Related Questions