Important: Please read the Qt Code of Conduct -

Connecting to QML signal function based syntax

  • Hi all,

    I'm trying to connect a QML signal to a C++ slot and want to use the function-based syntax:

    connect(sender, &Sender::valueChanged,  receiver, &Receiver::updateValue );

    Here is the explanation how to do it string-based and it works for me, but what would be the &Sender in this example?

  • Moderators

    @guy-incognito The examples in that link both use the old style of connecting signals/slots using SIGNAL() and SLOT() macros.

    The style you posted is the new style. In this case &Sender::valueChanged is a pointer to a function valueChanged in a class (QObject dervied) called Sender.

    I'm not quite sure what your question is in order to give you more information.

  • @ambershark

    My question is, how would I connect the signal "qmlSignal" in the example to the slot "cppSlot" using the new style. For me it's not clear what the sender would be, because it's a signal from the QML file.

  • Moderators

    @guy-incognito So I'm not sure you can use the new style connect syntax with a QML object. The reason is you don't know what kind of object it is, just that it derives from QObject.

    For example, in the example you linked they get a pointer to the qml object with QObject *item = view.rootObject();. So all you have is a QObject. In this case QObject does not have a qmlSignal in it, so you cant just do connect(item, &QObject::qmlSignal, ...).

    I'm not sure if you're able to even get the object that was defined with qmlSignal so you won't be able to use the new style syntax.

    I'm new enough to qml that there may be a way to get that actual object. If there is, and let's call it MyQmlObject then you would just do something like:

    auto item = qobject_cast<MyQmlObject *>(view.rootObject());
    connect(item, MyQmlObject::qmlSignal, .....);

    If you can find a way to get that object type/definition then you can do it. If not you're going to have to use the old SIGNAL(), SLOT() style.

Log in to reply