How to emit signal in context of specific QML Item from C++ code

Dariusz Walczak

I have two QML items with the same interface (signals emitted and handled) and different objectName property values:

Text {
    id: the_first_item
    objectName: "the_first_item"

    signal ping();

    onPing: {
        text = "pong! (variant 1)"
    }
}

Text {
    id: the_second_item
    objectName: "the_second_item"

    signal ping();

    onPing: {
        text = "pong! (variant 2)"
    }
}

I want to emit the ping signal in context of one of these items. In QML i would do it this way:

Item {
    onSomething: the_first_item.ping()
}

The problem is that I want to do it from C++ code. I have a pointer to instance of the QQuickItem class retrieved using findChild method and value of objectName property.

The only solution for handling signals triggered from the C++ code I have found was to define its own QObject derivative, declare a signal method in its body and then simply invoke it in context of an instance of this class:

class SomeLogic : public QObject
{
public signals:
    void ping();

public:
    void doSth() { ping(); }
};

Then, put a pointer to instance of this class in the engine's root context and connect a handler to this signal in QML in a following way:

Text {
    id: the_first_item
    //objectName: "the_first_item"

    Connections {
        target: the_instance_of_some_logic_property_name
        onPing: {
            text = "pong!"
        }
    }
}

But it's not good if I understand things correctly, because if the_second_item is defined in the same way, both of them will handle the ping signal emitted by the_instance_of_some_logic_property_name and I would like to trigger only one of them.

Now, while I am writing it, I think that maybe providing an utility function in each of items and then emit the ping signal in its own context, like that:

Text {
    id: the_first_item
    objectName: "the_first_item"

    signal ping();

    onPing: {
        text = "pong!"
    }

    function emitPing() {
        ping()
    }
}

In a simplier case, the emitPing would be enough (I wouldn't have to define the signal nor handler - I will just set the text in the emitPing function) but if I understand things correctly, the difference between function and signal is that the function invocation is synchronous while the signal is asynchronous and for some reasons (transistions between GUI states initiated from QML but handled in C++) I would like it to be asynchronous. I would also want to avoid necessity to write the no-brainer emitPing function everywhere.

Question: Is there a way to emit the ping signal, from the C++ code, in context of the_first_item only?

Felix

Since it helped as a comment, now as an answere:

You can emit signals of any QObject using Qts MetaObject System. In this case, to emit the ping signal of the_first_item, just call QMetaObject::invokeMethod(the_first_item, "ping");

More informations about the whole meta object mechanisms can be found here: The Meta-Object System.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does an Item emit a signal when its position/size is changed in QML?

From Dev

In QT is it safe to emit a signal from a closeEvent function?

From Dev

Sending a signal to a QML item from C++ (Qt5)

From Dev

How to bind to root context object signal from QML

From Dev

QML,How to dynamicaly change Item of Repeater from C++

From Dev

How to connect a qml child component signal to a c++ slot

From Dev

QML On Item Changed Signal

From Dev

emit qml signal on start application

From Dev

How can I listen to a C++ signal from QML?

From Dev

C++ Qt: Redirect cout from a thread to emit a signal

From Dev

Passing Q_GADGET as signal parameter from C++ to QML

From Dev

How to emit signal in context of specific QML Item from C++ code

From Dev

How to bind to a signal from a delegate component within a ListView in QML

From Dev

Can we emit a signal from a public slot

From Dev

How to emit items from list with delay between each item?

From Dev

C++/QML: ListView is not updated on dataChanged signal from QAbstractListModel

From Dev

Qt - how to emit a signal from derived class?

From Dev

Emit safely a signal from a thread and connect it to a widget

From Dev

QML,How to dynamicaly change Item of Repeater from C++

From Dev

QML Cannot emit signal if it has same name as property

From Dev

emit qml signal on start application

From Dev

How to signal an R exception from C code

From Dev

QML - connect a signal from c++ to qml

From Dev

C++ Qt: Redirect cout from a thread to emit a signal

From Dev

Passing Q_GADGET as signal parameter from C++ to QML

From Dev

How to emit items from list with delay between each item?

From Dev

How to emit same signal for 2 classess from QThread

From Dev

Qt emit signal from a class to class

From Dev

How set C++ class to an Item in qml?

Related Related

  1. 1

    Does an Item emit a signal when its position/size is changed in QML?

  2. 2

    In QT is it safe to emit a signal from a closeEvent function?

  3. 3

    Sending a signal to a QML item from C++ (Qt5)

  4. 4

    How to bind to root context object signal from QML

  5. 5

    QML,How to dynamicaly change Item of Repeater from C++

  6. 6

    How to connect a qml child component signal to a c++ slot

  7. 7

    QML On Item Changed Signal

  8. 8

    emit qml signal on start application

  9. 9

    How can I listen to a C++ signal from QML?

  10. 10

    C++ Qt: Redirect cout from a thread to emit a signal

  11. 11

    Passing Q_GADGET as signal parameter from C++ to QML

  12. 12

    How to emit signal in context of specific QML Item from C++ code

  13. 13

    How to bind to a signal from a delegate component within a ListView in QML

  14. 14

    Can we emit a signal from a public slot

  15. 15

    How to emit items from list with delay between each item?

  16. 16

    C++/QML: ListView is not updated on dataChanged signal from QAbstractListModel

  17. 17

    Qt - how to emit a signal from derived class?

  18. 18

    Emit safely a signal from a thread and connect it to a widget

  19. 19

    QML,How to dynamicaly change Item of Repeater from C++

  20. 20

    QML Cannot emit signal if it has same name as property

  21. 21

    emit qml signal on start application

  22. 22

    How to signal an R exception from C code

  23. 23

    QML - connect a signal from c++ to qml

  24. 24

    C++ Qt: Redirect cout from a thread to emit a signal

  25. 25

    Passing Q_GADGET as signal parameter from C++ to QML

  26. 26

    How to emit items from list with delay between each item?

  27. 27

    How to emit same signal for 2 classess from QThread

  28. 28

    Qt emit signal from a class to class

  29. 29

    How set C++ class to an Item in qml?

HotTag

Archive