Get focus on a popup
-
In order to have a QPushButton with a down arrow I have used QPushButton.setMenu to add a dummy menu to the button. Then I show a popup (QWidget based custom widget) on the aboutToShow() signal of the dummy menu. This works except the popup does not get the focus and must be clicked twice, once for focus, once again to activate button on the popup. The focus appears to remain with the QPushButton as its appearance changes when I click in my popup widget.
Is there a way that I can force the popup to get the focus when it is shown?
I have tried setFocus() but that doesn't work. If I remove the menu from my button and open the popup from the clicked() signal then it works properly, but I don't want to do that unless I can somehow also have the same down arrow on the button that you get when you add a menu.
-
That's weird. It should get focus when the widget is shown.
Can you try with just a simple QLabel or QPushButton or something instead of your custom widget. So when you would normally show your custom widget show a QPushButton instead as a test and see if it gets focus.
That will narrow down the problem on whether it's your custom widget or how it is being displayed.
-
I tried what you suggested using a QPushButton and it turns out that the behaviour I am seeing is caused by setting the Qt::Popup flag. If I use Qt::FramelessWindowHint instead then the popup gets the focus on opening but doesn't close when another widget gets the focus. This is the same whether I use my custom widget or QPushButton.
Also this is on OSX 10.8 with PyQt and Qt 5.2.1, in case that matters.
-
Can I see the code where you open the popup?
I've never used PyQt but it shouldn't really matter for what we're talking about here. And 5.2.1 is new enough for bug fixes and such. I have used popups with Qt 5.2.1 and OSX 10.9 and haven't seen this behavior.
-
Here is a simple example that shows the problem:
@
import sys
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import Qtapp = QtWidgets.QApplication(sys.argv)
title = "Focus test"
w = QtWidgets.QMainWindow()
w.setWindowTitle(title)def onAboutToShow():
p = QtWidgets.QPushButton(w)
p.setText("A button")
p.setWindowFlags(Qt.Popup)
p.move(QtGui.QCursor.pos())
p.show()b = QtWidgets.QPushButton()
m = QtWidgets.QMenu()
b.setMenu(m)
m.aboutToShow.connect(onAboutToShow)w.setCentralWidget(b)
w.show()
app.exec_()
@ -
I'll write some test code tomorrow and see if I can find out why this is happening. Don't have time to do it tonight. :)