I have a custom QWidget with the Qt::Popup flag set.
I have a button (QPushButton) that calls 'show' on the custom popup.

If I open the popup, and click outside the popup, the popup rightly closes.

However, if I open the popup and click on the "Show" button again (while the popup is open), to hide the popup, the following events occur:
1) MousePressDown occurs, hiding the popup
2) MousePressUp triggers the "show" button, instantly showing the popup again

This is not my desired behavior for the button. If, for example, I click on QtCreator's menubar or the open document dropdown, they show their popups. And if I click outside that popup onto a different widget, the popup is closed and that other widget is interacted with. But if I click off of the popup onto the button that originally showed it, the popup is properly hidden.

I also tried checking popup->isVisible() inside the show button's 'clicked' signal, but the popup gets hidden (on the mouse button press) before the clicked signal gets sent (on the mouse button release).

How do I get the same behavior with my own button + popup widget?

I want:
A) Clicking outside of the popup to interact normally with whatever gets clicked on.
B) Clicking on the button that shows the popup, while the popup is open, should hide the popup.

Currently I can't think of any way to get this behavior. My current hacky "solution" is catching hideEvent() on the popup, starting an elapsed timer, and ignoring calls to show() only if the elapsed time since the last 'hide' is more than 500 miliseconds. What's a more correct solution?