Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set input focus to a shown dialog in Qt?

Tags:

c++

windows

qt

In a button click slot, I create and exec() a dialog with a NULL parent. Inside the dialog's constructor, I have:

this->activateWindow();
this->raise();
this->setFocus();

The dialog is application modal and has strong focus. However, it does NOT respond to keyboard events until I click on it. How do I make the dialog get focus without having to click it?

like image 442
Jake Petroules Avatar asked May 25 '10 08:05

Jake Petroules


1 Answers

The problem was that I was setting the Qt:Tool window flag. Using Qt::Popup or Qt::Window instead will cause input focus is automatically set when the dialog is shown.

I used Qt::Window myself. Some of the other flags will probably work as well, but the main thing is that a QDialog with the Qt::Tool flag will not automatically set input focus when the dialog is shown.

like image 65
Jake Petroules Avatar answered Sep 28 '22 18:09

Jake Petroules