Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate window

Tags:

c++

qt

show

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
like image 752
Max Frai Avatar asked Jun 27 '09 15:06

Max Frai


People also ask

Can I activate window for free?

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.

What is activate window in computer?

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.

How can I activate my Windows 10?

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.


2 Answers

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.

like image 160
stephan Avatar answered Oct 26 '22 22:10

stephan


Try to use QApplication::setActiveWindow()

like image 43
e.tadeu Avatar answered Oct 26 '22 23:10

e.tadeu