Main widget steals focus after execution QAction. I need the focus to be set to popup widget.
QAction *action = new QAction(tr("show popup"), this);
connect(action, &QAction::triggered, this, &MyWidget::showPopup);
addAction(action);
void MyWidget::showPopup()
{
QMessageBox* popup = new QMessageBox(this);
popup->setModal(true);
popup->show();
popup->setFocus();
}
MyWidget inherits from QWidget.
Because you just created popup
, it's not 'there' yet in the GUI. Even the show()
doesn't instantly show it. After you leave the scope of MyWidget::showPopup()
, the GUI event loop will continue looping and be able to process your new popup. Thus the setFocus()
call comes too early.
But there is help underway:
QWidget::setFocus()
is a slot, so you can invoke it.
If you use a timer (QTimer::singleShot(0, popup, SLOT(setFocus()));
), it should work.
Maybe you'll need to use 10ms instead of 0ms.