Note that there are some explanatory texts on larger screens.

plurals
  1. POQToolButton pops up menu only when pressed and HOLD
    primarykey
    data
    text
    <p>I'm trying to implement an "open" button such that if a user presses it and holds, a menu of applications for the user to choose from will pop up; but as soon as the user releases the mouse, the menu should disappear. if the user releases the mouse without choosing an application on the menu, it should open the file with the default application. I'm implementing this button as a <code>QToolButton</code> and connect the signals as follows:</p> <pre><code>self.ui_open_btn.pressed.connect(self._onOpenBtnPressed) self.ui_open_btn.triggered.connect(self._onOpenBtnTriggered) def _onOpenBtnPressed(self): self.ui_open_btn.showMenu() def _onOpenBtnTriggered(self, action): application_name = action.text() # code to launch the application </code></pre> <p>Right now, when the user presses the button, the menu will pop up. However, the menu is still there when the user release the button, and action on the menu is triggered by clicking on it. I tried under both <code>DelayedPopup</code> and <code>InstantPopup</code> mode. As long as the menu has been set for <code>self.ui_open_btn</code>, I can no longer catch any <code>released</code> signal. How can I hide the menu when the user releases the mouse? How can the action on the menu be triggered by releasing the mouse?</p> <p>--added---</p> <p>I found another problem about using <code>QToolButton</code>: the menu always pops up when the button is pressed. Instead, I would like to catch the <code>pressed</code> signal, do some check to determine if the menu should pop up or not. So I changed my approach to write my customized toolbutton by subclassing <code>QPushButton</code> and <code>QMenu</code>. Please see my code posed in the answer below.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload