[Qt-qml] A simple, complete example of QML onvoking C++?

Colin Kern colin.kern at gmail.com
Tue Apr 13 19:01:59 CEST 2010


Hi Peter,

I replied a few minutes ago to your other email with some hopefully
useful information.  Just to expand on that with your specific example
here.

foo.h:

#include <QObject>

class Foo : public QObject
{
    Q_OBJECT

    public slots:
        void bar();
};

main.cpp:

#include "foo.h"
#include <QDeclarativeEngine>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QDeclarativeEngine *engine = new QDeclarativeEngine;
    QDeclarativeComponent component(engine, QUrl("myqml.qml"));
    QDeclarativeContext *context = new
QDeclarativeContext(engine->rootContext());
    context->setContextProperty("myFoo", new Foo);
    QObject *window = component.create(context);

    return app.exec();
}

myqml.qml:

import Qt 4.6

Component {
    MouseArea {
        anchors.fill: parent
        onClicked: myFoo.bar();
    }
}


I'm currently not at a computer where I can test this code, so I may
have made some syntax errors or something, but I think you get the
idea.  If the C++ function you want to call is just a utility function
and not really a method of an object, I think you'll still need to
just create a class called Util or something and then make those
functions public slots.  I've never tried to bind a global C++
function to a QtScript property before.

Colin


On Mon, Apr 12, 2010 at 8:57 PM, Peter Matuchniak
<pmatuchniak at imsco-us.com> wrote:
> I’m trying to follow the examples in the following link:
>
>     http://qt.nokia.com/doc/4.7-snapshot/qtbinding.html
>
>
>
> However they don’t seem complete enough to me, or I am missing some
> assumption that is not obvious to me  (given that I am new to Qt and QML).
>
>
>
> All I really want to do is have the simplest but complete example of the
> following:
>
> -          A QML button that, when pressed, invokes a C++ function
>
>
>
> Has anyone done this?
>
>
>
>
>
> __________________________________________________________________
>
> This email and any files transmitted with it are confidential & proprietary
> to Systems and
> Software Enterprises, Inc. (dba IMS). This information is intended solely
> for the use of
> the individual or entity to which it is addressed. Access or transmittal of
> the information
> contained in this e-mail, in full or in part, to any other organization or
> persons is not
> authorized.
>
> __________________________________________________________________
>
>
>
>
>
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-qml
>
>




More information about the Qt-qml mailing list