i have a QMainWindow. It has this parameters:
this->setWindowFlags(Qt::Tool); this->setFocusPolicy(Qt::StrongFocus); this->setAttribute(Qt::WA_QuitOnClose,true);
After showEvent calles my window is shown but unactivated.
I tried to overload show function:
...
QMainWindow::showEvent(event);
this->activateWindow();
...
But it doesn’t help me.
EDIT:
When i commented line
this->setWindowFlags(Qt::Tool);
everything worked fine, but i need in tool-flag.
Any ideas?
EDIT:
- OS: Linux
- Programming language: c++
- Qt version: 4.5.1
The Windows Manager Decides
Before I start: As pointed out by elcuco and Javier, focus policy and other aspects of the windows layout (e.g. the title bar) belongs to a substantial extend to the respective windows manager, and Qt might have limited control. To see this, just look at a user interface that has a “focus follows mouse” policy. In these cases, the windows manager might ignore Qt’s focus request. For this reasons, the Qt documentation calls many of the respective flags “hints”. Consequently, some of the suggested solutions might or might not work for you.
QApplication::setActiveWindow()
This not withstanding, e.tadeu’s solution to use
QApplication::setActiveWindow()works for me for both Windows and Ubuntu with Gnome. I tested it with the following code. Apologies that it is Python using PyQt (I use questions like these to learn a bit about PyQt). It should be fairly easy for you to read it and translate it into C++.Note that you have to add some handling of the close event of the
testWindow, because the app does not exit automatically if you close aQt::Toolwindow.The grabKeyboard() Hack
If this does not work for you, the following hack might. I assume that you have a window in your application that is active. You can then use
grabKeyboard()to redirect the input. TheQt::Toolwindow doesn’t get the focus, but receives the input. The following main code demonstrates it (the other code remains unchanged).Basically, while the window
testWindow2is the active one, all text entered shows up intestWindow.textEdit. It is not nice, I know…Creating Your Own Window
You gain the most flexibility (and create the most work for yourself) by rolling out your own window layout. The idea is described in the following FAQ.
Other “Solutions”
You could directly call the respective window manager’s API function to get the desired result (clearly against the very reason for using Qt in the first place). You could also hack the Qt source code. For example, on Windows, Qt uses the
ShowWindow()function with aSW_SHOWNOACTIVATEflag, to show a window with styleWS_EX_TOOLWINDOWif you set theQt::Toolflag. You could easily replace theSW_SHOWNOACTIVATEwith whatever you want. Linux should be the same. Clearly also not recommended.