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. Do signal of global object could be connect to slot of other object?
QtWS25 Last Chance

Do signal of global object could be connect to slot of other object?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.0k Views
  • 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.
  • C Offline
    C Offline
    cokefish
    wrote on last edited by
    #1

    Do signal of global object could be connect to slot of other object?
    I found that the signal emitted could not trigger slot in other object.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi @cokefish, and welcome to the Qt Dev Net!

      @cokefish said in Do signal of global object could be connect to slot of other object?:

      Do signal of global object could be connect to slot of other object?
      I found that the signal emitted could not trigger slot in other object.

      Please describe how you create the global object.

      Remember: you must create the QCoreApplication/QGuiApplication/QApplication before you create any other QObject.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      C 1 Reply Last reply
      0
      • JKSHJ JKSH

        Hi @cokefish, and welcome to the Qt Dev Net!

        @cokefish said in Do signal of global object could be connect to slot of other object?:

        Do signal of global object could be connect to slot of other object?
        I found that the signal emitted could not trigger slot in other object.

        Please describe how you create the global object.

        Remember: you must create the QCoreApplication/QGuiApplication/QApplication before you create any other QObject.

        C Offline
        C Offline
        cokefish
        wrote on last edited by
        #3

        @JKSH
        I defined it out of main().

        JKSHJ 1 Reply Last reply
        0
        • C cokefish

          @JKSH
          I defined it out of main().

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @cokefish said in Do signal of global object could be connect to slot of other object?:

          @JKSH
          I defined it out of main().

          That's too vague.

          Please post your code -- show us how you created the global object, and how you connected the signals.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            connect is a static method so you can invoke QObject::connect() directly in the main

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            kshegunovK 1 Reply Last reply
            0
            • VRoninV VRonin

              connect is a static method so you can invoke QObject::connect() directly in the main

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              Yes, but I'm pretty sure JKSH is worried about the order of creation of QObjects instead. :)

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                Ok, thanks, got it.

                So the solution is declare a global QObject pointer instead of QObject instance and create the object on the heap from main

                Are you 110% sure you need a global object? it's almost never a good thing

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  Well, the "cleanest" solution I've come up with is to do what QCoreApplication does - set the reference in the constructor, e.g:

                  class MyGlobalObject
                  {
                  public:
                      MyGlobalObject()
                      {
                          Q_ASSERT(!reference);
                          reference = this;
                      }
                  
                      static MyGlobalObject * instance()
                      {
                          return reference;
                      }
                  private:
                      static MyGlobalObject * reference;
                  };
                  
                  MyGlobalObject * MyGlobalObject::reference = nullptr; //< This goes in the cpp obviously.
                  

                  Then one can create the object wherever convenient (i.e. main()'s stack comes to mind) and get the instance everywhere:

                  int main(int argc, char ** argv)
                  {
                       QApplication app(argc, argv);
                       MyGlobalObject global; //< Ensures the global object is created after the root QObject, and destroyed before
                  
                       // ...
                       return QApplication::exec();
                  }
                  

                  However using global objects is grossly discouraged for a number of reasons, discussed numerous times in this forum and elsewhere.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0

                  • Login

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