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. [solved] Is it safe to emit other instance's signal(s) ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.1k 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.
  • J Offline
    J Offline
    joonhwan
    wrote on last edited by
    #1

    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
    0
    • T Offline
      T Offline
      tomma
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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
        0
        • J Offline
          J Offline
          joonhwan
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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
            0

            • Login

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