Solved Usage of QGuiApplication::focusWindowChanged()
-
Hi,
I have GUI app and in my
mainwindow
class and I need to know when a focused window of my app has changed.
For this I'm going to connect signalfocusWindowChanged
with slot. But this signal is fromQGuiApplication
andQApplication
class and I have object of this clas only in mymain()
function:int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
What is simplest way to connect signal
focusWindowChanged
inmainwindow
class? Or maybe there are some other ways to solve my task? -
@Please_Help_me_D said in Usage of QGuiApplication::focusWindowChanged():
What is simplest way to connect signal focusWindowChanged in mainwindow class?
Do it in main? You have both, main window and QApplication instances there...
-
@Please_Help_me_D said in Usage of QGuiApplication::focusWindowChanged():
What is simplest way to connect signal focusWindowChanged in mainwindow class?
Do it in main? You have both, main window and QApplication instances there...
-
@jsulm thank you!
Inmain
should fits me needs I think.
And if not in main then I guess I have to reimplementQMainWindow
(inherit MyMainWindow class from it) class and pass a pointer ofQApplication
there? -
@Please_Help_me_D
You can do it @jsulm's way.However, so that you know you have an alternative and could do it in
MainWindow
if you prefer, there is always a globally accessible instance of your application available throughstatic
https://doc.qt.io/qt-5/qcoreapplication.html#instance. So inMainWindow
you could do your connect viaQGuiApplication *app = qobject_cast<QGuiApplication *>(QCoreApplication::instance()); connect(app, &QGuiApplication::focusWindowChanged, this, &MainWindow::slot);
This would allow if, for example, you wish to keep your
MainWindow::slot()
methodprivate
. -
@JonB thank you!
A half hour ago I was looking to some library's source code and there was such thing and I could understand how it connect this signal. The answer is that there is a global instance of application :) -
@Please_Help_me_D said in Usage of QGuiApplication::focusWindowChanged():
I have to reimplement QMainWindow
You already do, right?
There is no need to pass QApplication pointer to anywhere - you can get it everywhere in your app: https://doc.qt.io/qt-5/qcoreapplication.html#instance -
@jsulm no I didn't do that yet
Now I just started to tryQCoreApplication *QCoreApplication::instance()
-
@Please_Help_me_D said in Usage of QGuiApplication::focusWindowChanged():
no I didn't do that yet
Then what is MainWindow? Isn't it a subclass of QMainWindow?
-
@jsulm
MainWindow
It is a clas inherited fromQMainWindow
object. It is just standard Qt mainwindow that appears in Qt GUI project -
@Please_Help_me_D So, "And if not in main then I guess I have to reimplement QMainWindow (inherit MyMainWindow class from it)" is already the case.
-
@jsulm Yes, haven't thought of that :)
-
BTW, I think
QCoreApplication::instance()
is a mouthful. There are two shorter versions for that:qApp
andqGuiApp
. These will cast the instance toQApplication
andQGuiApplication
, respectively.