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:
Microsoft allows anyone to download Windows 10 for free and install it without a product key. It'll keep working for the foreseeable future, with only a few small cosmetic restrictions.
Activation helps verify that your copy of Windows is genuine and hasn't been used on more devices than the Microsoft Software License Terms allow.
Press the Windows key, then go to Settings > Update and Security > Activation. If Windows is not activated, search and press 'Troubleshoot'. Select 'Activate Windows' in the new window and then Activate. Or, select 'I changed hardware on this device recently', if applicable.
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++.
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self)
# main window
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Test')
# text editor
self.textEdit = QtGui.QTextEdit()
self.setCentralWidget(self.textEdit)
def closeEvent(self, event):
QtGui.QApplication.instance().quit()
#main
app = QtGui.QApplication(sys.argv)
testWindow = MainWindow()
testWindow.setWindowFlags(QtCore.Qt.Tool)
testWindow.show()
app.setActiveWindow(testWindow)
app.exec_()
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 a Qt::Tool
window.
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. The Qt::Tool
window doesn't get the focus, but receives the input. The following main code demonstrates it (the other code remains unchanged).
#main
app = QtGui.QApplication(sys.argv)
testWindow = MainWindow()
testWindow.setWindowFlags(QtCore.Qt.Tool)
testWindow2 = MainWindow() # second window which is active
testWindow2.show()
testWindow.show()
testWindow.textEdit.grabKeyboard()
app.exec_()
Basically, while the window testWindow2
is the active one, all text entered shows up in testWindow.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 a SW_SHOWNOACTIVATE
flag, to show a window with style WS_EX_TOOLWINDOW
if you set the Qt::Tool
flag. You could easily replace the SW_SHOWNOACTIVATE
with whatever you want. Linux should be the same. Clearly also not recommended.
Try to use QApplication::setActiveWindow()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With