Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved]What’s the difference between QMenu.popup and QMenu.exec_
Qt 6.11 is out! See what's new in the release blog

[Solved]What’s the difference between QMenu.popup and QMenu.exec_

Scheduled Pinned Locked Moved Language Bindings
3 Posts 3 Posters 13.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    redstoneleo
    wrote on last edited by
    #1

    What’s the difference between
    PySide.QtGui.QMenu.popup(pos[, at=None])¶

    PySide.QtGui.QMenu.exec_(pos[, at=None])

    It would be great to explain it by By example 

    Although the documentation says

    Context menus are usually invoked by some special keyboard key or by right-clicking. They can be executed either asynchronously with PySide.QtGui.QMenu.popup() or synchronously with exec() .

    But I still don’t understand

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      I'm afraid I run PyQt rather than PySide, but the following example should map pretty well:

      @
      from PyQt4.QtCore import *
      from PyQt4.QtGui import *

      class Widget(QWidget):
      def init(self, parent=None):
      QWidget.init(self, parent)

          self.setContextMenuPolicy(Qt.CustomContextMenu)
          self.customContextMenuRequested.connect(self.showMenu)
      
          l=QVBoxLayout(self)
          self.popup=QRadioButton("Popup", checked=True)
          self.exec__=QRadioButton("Exec_")
          l.addWidget(self.popup)
          l.addWidget(self.exec__)
      
      def showMenu(self, pos):
          menu=QMenu(self)
          menu.addAction(QAction("Item 1", menu))
          menu.addAction(QAction("Item 2", menu))
          menu.addAction(QAction("Item 3", menu))
          if self.popup.isChecked(): menu.popup(self.mapToGlobal(pos))
          else: menu.exec_(self.mapToGlobal(pos))
          print "Finished"
      

      if name=="main":
      from sys import argv, exit
      a=QApplication(argv)
      w=Widget()
      w.show()
      exit(a.exec_())
      @

      Basically, when you run this example you'll get two checkboxes: when "Popup" is checked the context menu will be invoked with popup() and when "Exec_" is checked the menu is invoked with exec_(). To actually invoke the menu, simply right click anywhere in the window.

      As to your actual question, the difference between popup() and exec_(): the answer was actually in your question, in that popup() is asynchronous and exec_() is synchronous. This means that when you use popup(), as soon as the menu is shown, execution of the application continues, as demonstrated by the fact that "finished" is printed into the terminal (almost) instantly. With exec_() however, "finished" is not printed into the terminal until the user either selects an action from the menu or clears it by clicking away/pressing esc. This is because exec_() blocks the main event loop and starts one of its own. This behavior is seen throughout Qt where widgets such as QDialog can either be displayed with show() or exec_().

      The upshot is, with popup() you use the signal/slot mechanism to determine which option the user picked (if any), i.e. the actions "triggered" signal will be fired which you connect to. With exec_(), the selected action is also returned by the function.

      Hope this helps.

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        From a user's point of view:

        In this case, word pairs with the same meaning:

        • asynch/synch
        • popup/exec
        • non-modal/modal

        Modal: when a thing ( dialog or in this case a menu) causes the GUI to enter a state where the user can do nothing else (other modes) in that app until the thing is finished.

        With some variations: if the user clicks outside a modal menu, it is finished and returns a null result. Whereas in a modal dialog, the user must click in a Cancel button to finish the dialog, and can't click elsewhere in your app with any effect.

        And even when an app is in a modal state, the user might be able to click in the windows of other apps (usually, it depends on the window manager, etc.)

        From a programmer's point of view, your app event loop doesn't receive events when a modal dialog or menu is displayed. (Qt does receive and handle events, but doesn't dispatch them to your event loop.)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved