Important: Please read the Qt Code of Conduct -

Using Qt c++ function in qml

  • I tried using a cpp function in my qml but am not able to do it properly.
    Here is the code

    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1

    Rectangle {
    width: 360
    height: 360
    visible: true
    Text {
    text: qsTr("Hello World")
    anchors.centerIn: parent
    MouseArea {
    anchors.fill: parent
    onClicked: {
    console.log("hello in qml");

    @#include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "product.h"
    #include <QDeclarativeEngine>
    #include <qmlapplicationviewer.h>
    #include <QDeclarativeContext>

    Q_DECL_EXPORT int main(int argc, char *argv[])
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    Product p;;
        QmlApplicationViewer viewer;
        QDeclarativeEngine *engine = viewer.engine();
        QDeclarativeContext *context = engine->rootContext();
        context->setContextProperty("main", engine);
    return app->exec&#40;&#41;;


    @#include "product.h"

    qDebug()<<"hello in constructor";
    void Product::abc(int a)
    qDebug()<<"a= "<<a;

    I know there should be error in using unknown type Product in qml but what i dont know is how to make it available there.
    Can anyone help me out.

  • You have to subclass QDeclarativeItem, or at least QObject, add use the Q_PROPERTY macro for all properties you need to access from QML and so on, read in detail here:

Log in to reply