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. Utilizing connect with the SIGNAL macro and a functor
Forum Updated to NodeBB v4.3 + New Features

Utilizing connect with the SIGNAL macro and a functor

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 542 Views 2 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.
  • V Offline
    V Offline
    VanDriekenof
    wrote on last edited by VanDriekenof
    #1

    Hello,

    I'm trying to dynamically connect a widget to a functor. I have a file that contains the signature of the signal that I want to connect, as well as some info concerning what it should do, something along the line of:

    "widget": "power_button",
    "signal": "toggled(bool)",
    "action": "power"

    As you can see, the signal is easy to connect, I can just call connect with the string (I just have to prepend a "2" and convert to const char*). I can't (at least easily) use the pointer-to-member syntax easily because those widgets are created at runtime and they don't have all the same signals (and signal signatures).

    So I would like to be able to call something like

    QObject::connect(widget, signal_string, [](bool a){ set("power", a); });
    

    but there is no connect overload like that, and I'm afraid the two syntaxes are fundamentally incompatible.

    Is there a way to do what I want, or will I have to do something a bit more complicated ?

    Thnaks a lot !

    raven-worxR 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to the forums.
      Sadly, macro syntax and lambdas cannot be mixed.
      https://wiki.qt.io/New_Signal_Slot_Syntax
      So I guess you are looking at something "bit more complicated "

      1 Reply Last reply
      4
      • V VanDriekenof

        Hello,

        I'm trying to dynamically connect a widget to a functor. I have a file that contains the signature of the signal that I want to connect, as well as some info concerning what it should do, something along the line of:

        "widget": "power_button",
        "signal": "toggled(bool)",
        "action": "power"

        As you can see, the signal is easy to connect, I can just call connect with the string (I just have to prepend a "2" and convert to const char*). I can't (at least easily) use the pointer-to-member syntax easily because those widgets are created at runtime and they don't have all the same signals (and signal signatures).

        So I would like to be able to call something like

        QObject::connect(widget, signal_string, [](bool a){ set("power", a); });
        

        but there is no connect overload like that, and I'm afraid the two syntaxes are fundamentally incompatible.

        Is there a way to do what I want, or will I have to do something a bit more complicated ?

        Thnaks a lot !

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @VanDriekenof
        i once wrote an utility class for this

        #include <functional>
        
        typedef std::function<void()> LamdbaFunc;
        
        class LambdaConnector : public QObject
        {
            Q_OBJECT
        
        public:
            LambdaConnector( QObject* obj, const char* signal, LambdaFunc functor ) : QObject(obj) {
                static QMetaMethod slotMethod;
                if( !slotMethod.isValid() )
                {
                        const QMetaObject * mo = this->metaObject();
                        int idx = mo->indexOfSlot("onEmit()");
                        slotMethod = mo->method(idx);
                }
        
                QMetaMethod signalMethod = obj->metaObject()->method(obj->metaObject()->indexOfSignal(signal));
                connect( object, signalMethod, this, slotMethod );
           }
        
        private slots:
            void onEmit() {
               m_SlotFunction();
           }
        
        private:
            LambdaFunc m_SlotFunction;
        };
        

        Usage:

        new LambdaConnector(obj, "signal()", [obj]() {
            obj->...
        });
        // or
        new LambdaConnector(obj, QMetaObject::normalizedType("signal()").constData(), [obj]() {
            obj->...
        });
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        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