Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [solved] Is it safe to emit other instance's signal(s) ?

    General and Desktop
    3
    5
    1861
    Loading More Posts
    • 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.
    • J
      joonhwan last edited by

      I have client code like

      @
      class Hsm : public QObject
      {
      ...
      void handleInitEvent();
      };

      void Hsm::handleInitEvent()
      {
      emit gui->initCounter();
      }
      @

      and the signals' owner was written like this.

      @
      class GuiSignals : public QObject
      {
      ...
      signals:
      #ifndef Q_MOC_RUN
      public: // don't tell moc, doxygen or kdevelop, but those signals are in fact public
      #endif
      void initCounter(int value=10);
      };
      extern GuiSignals* gui; // assume this is instantiated beforehand somehow.
      @

      Please be noted that I introduced Q_MOC_RUN non-define'd region and put 'public' keyword there to make it compilable.
      (I've not been aware 'signals' keyword is defined as 'protected:' till now :-( )
      So far, slot gets be called but this is a kind of hack looking, and feels not that good. Is there any special reason that a signal is not in public?

      joonhwan at gmail dot com

      1 Reply Last reply Reply Quote 0
      • T
        tomma last edited by

        I would put GuiSignals as friend of Hsm if you need more of GuiSignals' signals emited from there. Or you can make public method GuiSignals::emitInitCounter(int) which handles emiting.

        1 Reply Last reply Reply Quote 0
        • A
          andre last edited by

          In principle, there can be good reasons for doing what you are doing, and I don't think it is wrong per-se. However, the fact that you need hacks to do it, might already indicate that you are in fact doing something non-standard.

          If possible without completely screwing up your overall design, I would recommend not to do this. Alternatively, create some methods on your GuiSignals class that allow triggering the slots. These may be public or perhaps protected (using fried like Tomma suggests).

          As an alternative approach to qmake/moc hacks, you can also use QMetaObject to trigger signals from outside of the object itself.
          @
          QMetaObject::invokeMethod(gui, "initCounter");
          @
          And yes, that is also a hack.

          1 Reply Last reply Reply Quote 0
          • J
            joonhwan last edited by

            Thanks guys. Though not figure out the reason of that standard('protected' signal only), tips were helpful.

            joonhwan at gmail dot com

            1 Reply Last reply Reply Quote 0
            • A
              andre last edited by

              On a side note: to support the template-based signal-slot syntax in Qt5, AFAIK signals will be public there by default.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post