Hi,
I'm trying to get an access to my c++ class from within QML. The idea is to run a function in C++ side when I press a rectangle area in QML. Also sending a signal from qml and catching it in c++ is an option. But all the examples I've found with google don't seem to work. I.e:
QDeclarativeView view;
view.
setSource(QUrl::fromLocalFile("main.qml"));
view.rootContext()->setContextProperty("stopwatch", new Stopwatch);
view.show();
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("main.qml"));
view.rootContext()->setContextProperty("stopwatch", new Stopwatch);
view.show();
To copy to clipboard, switch view to plain text mode
import Qt 4.7
Rectangle {
width: 300
height: 300
MouseArea {
anchors.fill: parent
onClicked: {
if (stopwatch.isRunning())
stopwatch.stop()
else
stopwatch.start();
}
}
}
import Qt 4.7
Rectangle {
width: 300
height: 300
MouseArea {
anchors.fill: parent
onClicked: {
if (stopwatch.isRunning())
stopwatch.stop()
else
stopwatch.start();
}
}
}
To copy to clipboard, switch view to plain text mode
this would give me an error: TypeError: Result of expression 'stopwatch.start' [undefined] is not a function.
Also one example of connecting signal/slot:
QDeclarativeView view;
view.
setSource(QUrl::fromLocalFile("main.qml"));
QObject *rootObject
= dynamic_cast<QObject
*>
(view.
rootObject());
QObject::connect(rootObject,
SIGNAL(someqmlfunc
()),
&this,
SLOT(somecpluplusfunc
()));
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("main.qml"));
QObject *rootObject = dynamic_cast<QObject*>(view.rootObject());
QObject::connect(rootObject, SIGNAL(someqmlfunc()), &this, SLOT(somecpluplusfunc()));
To copy to clipboard, switch view to plain text mode
but conversion from QGraphicsObject to QObject is not possible.
Is there some working way to call a c++ function from within qml at the moment?
Bookmarks