An easier way to connect



  • I've been wondering, the new connection syntax tends to be a little over-verbose, can't we make use of the MOC to make connections really simple.

    After all, the connection boils down to connecting the signal of an instance to the slot of an instance. I.e.

    @connect(&obj1, &T1::someSig, obj1Ptr, T2::someSlot);@

    Occasionally, you have to deal with specifying overloads, making the syntax a horrid mess.

    Now, how cool would it be if you could simply:

    @connect(obj1.someSig, obj2Ptr->someSlot)@

    Naturally, this could not happen in C++, but the MOC can be used to generate the actual full connection syntax from this, with convenience features like:

    • automatically distinguish between references and pointers, using . for member access signifies the MOC needs to add the & operator before the object identifier

    • automatically handle necessary overloads, the user can use parameters to specify it, i.e. @obj.someSig(int)
      // vs
      obj.someSig(QString)@

    Instead of using connect() a macro can be used, such as:

    @Q_CONNECT(spinbox->valueChanged(int), slider->setValue)
    //
    // vs the current syntax, requiring explicit cast due to valueChanged() being overloaded
    //
    connect(spinbox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), slider, &QSlider::setValue);@

    Naturally, I don't put this in the context of a feature suggestion, the point is to get some feedback what people think before considering making it a feature suggestion.


  • Moderators

    I think it is a good idea! Writing connect statements (both old and new syntax) is always a bit painful. Your solution would make it clean and simple.



  • I have created a "suggestion for the feature":https://bugreports.qt-project.org/browse/QTBUG-43087, vote for it to hopefully see it implemented sooner.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.