Communication between C++ and QML

This page shows how to call C++ functions from within QML.

What I want to do is change the image on a Button via a C++ function (trigger a state-change or however it is done).

How can I achieve this?

UPDATE

I tried the approach by Radon, but immediately when I insert this line:

    QObject *test = dynamic_cast(viewer.rootObject());

Compiler complains like this:

    error: cannot dynamic_cast '((QMLCppBinder*)this)->QMLCppBinder::viewer.QDeclarativeView::rootObject()' (of type 'struct QGraphicsObject*') to type 'class QObject*' (source is a pointer to incomplete type)

In case it is relevant, QMLCppBinder is a class that I try to build to encapsulate the connections from several QML pages to C++ code. Which seems to be trickier than one might expect.

Here is a skeleton class to give some context for this:

    class QMLCppBinder : public QObject
    {
        Q_OBJECT
    public:
        QDeclarativeView viewer;

        QMLCppBinder() {
            viewer.setSource(QUrl("qml/Connect/main.qml"));
            viewer.showFullScreen();
            // ERROR
            QObject *test = dynamic_cast(viewer.rootObject());
        }
    }

11
задан tom redfern 14 September 2011 в 09:20
поделиться