[Solved] C++ Signals for QML Layer



  • Hi,

    I have a question about the signal connection between my C++ logic layer and my QML visual Layer.

    What I need is to connect my interface class with the QML layer, I found some tutorials about that topic.
    The answer seems to be the

    qmlRegisterType<QmlInterface >("QmlInterface ", 1, 0, "Interface ");

    statement.
    Ok so far so good, but in this examples I'm missing one detail.

    Lets take the QmlInterface class. In the example the QmlInterface class is just one independent class with no connection
    to other classes or the rest of the C++ logic.

    In my current project I have a QmlInterface class which is designed to send diffenrent events and informations to my QML layer.
    This QmlInterface needs a pointer to my CoreEngine which is handling the different events. But I can't figure out how I can
    use signals of my QmlInterface class in the QML layer.

    Lets say this is a pretty primitive variant of my QmlInterface class. I instantiate this class so far to set an index from a CoreEngine
    when the index is changed I want to emit a Signal indexChanged() so that the QML layer know that the index was changed.

    This class should work for the qmlRegisterType approach.
    But my QmlInterface needs a pointer to the CoreEngine.

    Until now the QmlInterface class has a parameter CoreEngine *core for the interaction with the core of my app.
    But this isn't working when I use the qmlRegisterType().

    So what can I do to realize this connection from CoreEngine-> to QmlInterface -> to QML-layer ?

    I hope this isn't to confusing and you nearly understand whats my issue.

    @

    class QmlInterface : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(int index READ index NOTIFY indexChanged)

    public:
    QmlInterface(QObject *parent=0) : QObject(parent)
    {
    /some stuff/
    this->m_index = 1;
    }

     int index() const { return this->index; }
    
     void changeIndex(int index)
     {
         this->m_index = index;
         emit indexChanged();
     }
    

    signals:
    void indexChanged();

    private:
    int m_index;
    };

    @



  • Here are a couple things you could try:

    Michael



  • Hi, I used setContextProperty() already but I didn't get access to the signals maybe I have done something wrong.

    But I don't understand your idea with the static instance() method.
    What should the instance() method return ? a pointer to the Core ?

    But how can a static method return a pointer to its object ? As far as I know static methods have no specific object.

    Maybe you can tell me what instance() should do in your opinion.

    Jan



  • [quote author="Schneidi" date="1283522910"]Hi, I used setContextProperty() already but I didn't get access to the signals maybe I have done something wrong.[/quote]

    How did you try to access the signals? Assuming you exposed your class as "myInterface", and it had a signal named "mySignal()", the following should work:

    @Connections {
    target: myInterface
    onMySignal: ...
    }@

    [quote author="Schneidi" date="1283522910"]But I don't understand your idea with the static instance() method.
    What should the instance() method return ? a pointer to the Core ?

    But how can a static method return a pointer to its object ? As far as I know static methods have no specific object.

    Maybe you can tell me what instance() should do in your opinion.[/quote]

    Sorry, I should have been more explicit. I was thinking you could use the Singleton pattern, if it made sense in the context of your application. So something like:

    @CoreInstance *CoreInstance::instance()
    {
    static CoreInstance *myInstance = 0;
    if (!myInstance)
    myInstance = new CoreInstance();
    return myInstance;
    }@

    Michael



  • Hi,

    thank you Micheal the Connections {} utility was exactly what I needed.

    The Singleton isn't really suitable for this case.



  • Hi.

    Like qml use QtSCript you can use connect function

    @myObject.mySignal.connect(...)@

    "http://doc.trolltech.com/4.6/scripting.html#signal-to-function-connections":http://doc.trolltech.com/4.6/scripting.html#signal-to-function-connections

    Normally it's works.

    [edit: fixed link / $chetankjain]



  • Thanks for the hint.

    I didn't use QtScript yet maybe I should take a look at it. ^^


Log in to reply
 

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