Usage of QGuiApplication::focusWindowChanged()
-
Hi,
I have GUI app and in my
mainwindowclass and I need to know when a focused window of my app has changed.
For this I'm going to connect signalfocusWindowChangedwith slot. But this signal is fromQGuiApplicationandQApplicationclass 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
focusWindowChangedinmainwindowclass? Or maybe there are some other ways to solve my task? -
Hi,
I have GUI app and in my
mainwindowclass and I need to know when a focused window of my app has changed.
For this I'm going to connect signalfocusWindowChangedwith slot. But this signal is fromQGuiApplicationandQApplicationclass 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
focusWindowChangedinmainwindowclass? 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!
Inmainshould 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 ofQApplicationthere? -
Hi,
I have GUI app and in my
mainwindowclass and I need to know when a focused window of my app has changed.
For this I'm going to connect signalfocusWindowChangedwith slot. But this signal is fromQGuiApplicationandQApplicationclass 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
focusWindowChangedinmainwindowclass? Or maybe there are some other ways to solve my task?@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
MainWindowif you prefer, there is always a globally accessible instance of your application available throughstatichttps://doc.qt.io/qt-5/qcoreapplication.html#instance. So inMainWindowyou 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. -
@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
MainWindowif you prefer, there is always a globally accessible instance of your application available throughstatichttps://doc.qt.io/qt-5/qcoreapplication.html#instance. So inMainWindowyou 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 :) -
@jsulm thank you!
Inmainshould 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 ofQApplicationthere?@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 -
@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() -
@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?
-
@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
MainWindowIt is a clas inherited fromQMainWindowobject. It is just standard Qt mainwindow that appears in Qt GUI project -
@jsulm
MainWindowIt is a clas inherited fromQMainWindowobject. 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.
-
@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:qAppandqGuiApp. These will cast the instance toQApplicationandQGuiApplication, respectively.