Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Usage of QGuiApplication::focusWindowChanged()
Forum Updated to NodeBB v4.3 + New Features

Usage of QGuiApplication::focusWindowChanged()

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by
    #1

    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 signal focusWindowChanged with slot. But this signal is from QGuiApplication and QApplication class and I have object of this clas only in my main() 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 in mainwindow class? Or maybe there are some other ways to solve my task?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • Please_Help_me_DP Please_Help_me_D

      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 signal focusWindowChanged with slot. But this signal is from QGuiApplication and QApplication class and I have object of this clas only in my main() 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 in mainwindow class? Or maybe there are some other ways to solve my task?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Please_Help_me_DP 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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_DP Offline
        Please_Help_me_DP Offline
        Please_Help_me_D
        wrote on last edited by
        #3

        @jsulm thank you!
        In main should fits me needs I think.
        And if not in main then I guess I have to reimplement QMainWindow (inherit MyMainWindow class from it) class and pass a pointer of QApplication there?

        jsulmJ 1 Reply Last reply
        0
        • Please_Help_me_DP Please_Help_me_D

          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 signal focusWindowChanged with slot. But this signal is from QGuiApplication and QApplication class and I have object of this clas only in my main() 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 in mainwindow class? Or maybe there are some other ways to solve my task?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 through static https://doc.qt.io/qt-5/qcoreapplication.html#instance. So in MainWindow you could do your connect via

          QGuiApplication *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() method private.

          Please_Help_me_DP 1 Reply Last reply
          1
          • JonBJ JonB

            @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 through static https://doc.qt.io/qt-5/qcoreapplication.html#instance. So in MainWindow you could do your connect via

            QGuiApplication *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() method private.

            Please_Help_me_DP Offline
            Please_Help_me_DP Offline
            Please_Help_me_D
            wrote on last edited by
            #5

            @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 :)

            1 Reply Last reply
            0
            • Please_Help_me_DP Please_Help_me_D

              @jsulm thank you!
              In main should fits me needs I think.
              And if not in main then I guess I have to reimplement QMainWindow (inherit MyMainWindow class from it) class and pass a pointer of QApplication there?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Please_Help_me_DP 1 Reply Last reply
              1
              • jsulmJ jsulm

                @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_DP Offline
                Please_Help_me_DP Offline
                Please_Help_me_D
                wrote on last edited by
                #7

                @jsulm no I didn't do that yet
                Now I just started to try QCoreApplication *QCoreApplication::instance()

                jsulmJ 1 Reply Last reply
                0
                • Please_Help_me_DP Please_Help_me_D

                  @jsulm no I didn't do that yet
                  Now I just started to try QCoreApplication *QCoreApplication::instance()

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Please_Help_me_DP 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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_DP Offline
                    Please_Help_me_DP Offline
                    Please_Help_me_D
                    wrote on last edited by
                    #9

                    @jsulm MainWindow It is a clas inherited from QMainWindow object. It is just standard Qt mainwindow that appears in Qt GUI project

                    jsulmJ 1 Reply Last reply
                    0
                    • Please_Help_me_DP Please_Help_me_D

                      @jsulm MainWindow It is a clas inherited from QMainWindow object. It is just standard Qt mainwindow that appears in Qt GUI project

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Please_Help_me_DP 1 Reply Last reply
                      2
                      • jsulmJ jsulm

                        @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_DP Offline
                        Please_Help_me_DP Offline
                        Please_Help_me_D
                        wrote on last edited by
                        #11

                        @jsulm Yes, haven't thought of that :)

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #12

                          BTW, I think QCoreApplication::instance() is a mouthful. There are two shorter versions for that: qApp and qGuiApp. These will cast the instance to QApplication and QGuiApplication, respectively.

                          1 Reply Last reply
                          2

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved