5

I want to pass some parameters from C++ to QML, so that QML can do something with them.

Somewhat like this:

void MyClass::myCplusplusFunction(int i, int j)
{
    emit mySignal(i, j);
}

In QML, every time that mySignal(i, j) is emitted, I want to call a QML function and do stuff with i and j.

Connections {
    target: myClass
    // mySignal(i, j) is emitted, call myQmlFunction(i,j)
}

How would I go about doing that?

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
Don Joe
  • 274
  • 4
  • 13
  • https://stackoverflow.com/questions/8834147/c-signal-to-qml-slot-in-qt – Antoine Morrier Aug 16 '17 at 14:48
  • 2
    Possible duplicate of [C++ SIGNAL to QML SLOT in Qt](https://stackoverflow.com/questions/8834147/c-signal-to-qml-slot-in-qt) – eyllanesc Aug 17 '17 at 00:21
  • @eyllanesc: This is by no means a duplicate of the linked question. It is just related. In the question you linked, the OP tries to establish the connection on the C++-side. This question is about connecting on the QML side. – derM - not here for BOT dreams Aug 17 '17 at 07:55

2 Answers2

5

Let's say you have a signal in cpp side:

void yourSignal(int i, QString t)

You have two options:

  • register your class as a qml type and use it as a qml object. The object will be initialized from QML side. reference:

    qmlRegisterType<ClassNameCPP>("com.mycompany.qmlName", 1, 0, "ClassNameQml");

Then, in qml:

import QtQuick 2.9
import com.mycompany.qmlName 1.0

Item{
    ClassNameQml{
        id: myQmlClass
        onYourSignal: {
            console.log(i,t); // Do whatever in qml side
        }
    }
}
  • add your class as a qml variable. This option is preferred when you need reuse your object several times. reference:

    view.rootContext()->setContextProperty("varName", &cppObject);

Then, in qml:

import QtQuick 2.9
Item{
    Connections{
        target: varName
        // In QML for each signal you have a handler in the form "onSignalName"
        onYourSignal:{
            // the arguments passed are implicitly available, named as defined in the signal
            // If you don't know the names, you can access them with "arguments[index]"
            console.log(i,t); // Do whatever in qml side
        }
    }
}
albertTaberner
  • 1,969
  • 1
  • 26
  • 35
  • 1
    I have added some QML code. I think this answer it's exactly what Don Joe asks for, using the two possible scenerios. – albertTaberner Aug 21 '17 at 08:16
  • 1
    Now you got it right, I think. +1 from me, especially as I think it is unlikely that the OP will show up again to accept it. I added two comments in your code with some more explaination, I hope you are all right with it. – derM - not here for BOT dreams Aug 21 '17 at 08:18
-1

You can find the whole documentation here:

http://doc.qt.io/qt-4.8/qtbinding.html

Stefano
  • 3,981
  • 8
  • 36
  • 66
  • 5
    As the link might go dead (especially as you point to the old Qt4.8 documentation) it would be highly appreciated, if you could expand your answer in a way that it includes all relevant details to answer the question. The link should be only considered *additional*. – derM - not here for BOT dreams Aug 17 '17 at 07:48