×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Regular Contributor
    Join Date
    Nov 2011
    Location
    Tunisia
    Posts
    81

    QML & Qt C++ : ReferenceError [Resolved]

    Hi,
    I'm writing a QtQuick app, and I need to use some Qt C++ functions, but when I try to access to my class method I get this error: file:///C:/QtDev/test022-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/qml/test022/MainPage.qml:20: ReferenceError: Can't find variable: TestClass

    I also tried with a small example (the source code is below), and I get the same error, please someone have an idea why I get this error?

    main.cpp
    Code:
    #include <QtGui/QApplication>
    #include <QtDeclarative>
    #include "qmlapplicationviewer.h"
    #include "testclass.h"
    
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
        QScopedPointer<QApplication> app(createApplication(argc, argv));
        
        TestClass testClass;
    
        QmlApplicationViewer *viewer = new QmlApplicationViewer();
    
        QDeclarativeContext *context = viewer->rootContext();
        context->setContextProperty("TestClass", &testClass);
        viewer->setMainQmlFile(QLatin1String("qml/test022/main.qml"));
        viewer->showExpanded();
    
        return app->exec();
    }

    testclass.h
    Code:
    #ifndef TESTCLASS_H
    #define TESTCLASS_H
    
    #include <Qt>
    #include <QObject>
    #include <QDebug>
    #include <QtDeclarative>
    
    
    class TestClass : public QObject
    {
        Q_OBJECT
    public:
        TestClass(){}
        Q_INVOKABLE QString showText(QString txt){
    
            qDebug() << txt;
    
            QDeclarativeEngine *engine = new QDeclarativeEngine;
            QDeclarativeComponent component(engine, QUrl::fromLocalFile("qml/test022/MainPage.qml"));
            if (component.status() == 1){
                QObject *object = component.create();
                QVariant returnedValeur;
                QVariant msg = "002";
                QMetaObject::invokeMethod(object, "jsFunction",Q_RETURN_ARG(QVariant, returnedValeur), Q_ARG(QVariant, msg));
                qDebug() << returnedValeur.toString();
            }
            engine->deleteLater();
            return "OK";
        }
    };
    
    #endif // TESTCLASS_H

    main.qml
    Code:
    import QtQuick 1.1
    import com.nokia.symbian 1.1
    import com.nokia.extras 1.1
    
    PageStackWindow {
        id: window
        initialPage: MainPage {tools: toolBarLayout}
        showStatusBar: true
        showToolBar: true
    
        ToolBarLayout {
            id: toolBarLayout
            ToolButton {
                flat: true
                iconSource: "toolbar-back"
                onClicked: window.pageStack.depth <= 1 ? Qt.quit() : window.pageStack.pop()
            }
        }
    }

    MainPage.qml
    Code:
    import QtQuick 1.1
    import com.nokia.symbian 1.1
    import com.nokia.extras 1.1
    
    Page {
        id: mainPage
    
        function jsFunction(jsVar){
            console.log(jsVar)
            return "003"
        }
    
        Text {
            anchors.centerIn: parent
            text: qsTr("Hello world!")
            color: platformStyle.colorNormalLight
            font.pixelSize: 20
        }
        Component.onCompleted: TestClass.showText("001")
    }
    Last edited by aymen_ka; 2013-03-22 at 23:52.

  2. #2
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Gurgaon,India
    Posts
    4,101

    Re: QML & Qt C++ : ReferenceError

    A work around is given here which you can try : http://stackoverflow.com/questions/8...ml-on-nokia-n9

  3. #3
    Regular Contributor
    Join Date
    Nov 2011
    Location
    Tunisia
    Posts
    81

    Re: QML & Qt C++ : ReferenceError

    Hi, thank you for your response
    But I tried this and it doesn't work, I get the same error

  4. #4
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Gurgaon,India
    Posts
    4,101

    Re: QML & Qt C++ : ReferenceError

    I think TestClass will only be visible in main.qml & not in MainPage.qml. What happens if you try accessing TestClass in main.qml?

  5. #5
    Regular Contributor
    Join Date
    Nov 2011
    Location
    Tunisia
    Posts
    81

    Re: QML & Qt C++ : ReferenceError

    Correct, I can access to TestClass in main.qml without errors, but how I can access to it from MainPage.qml ?

  6. #6
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Gurgaon,India
    Posts
    4,101

    Re: QML & Qt C++ : ReferenceError

    You can refer here as how c++ is exposed from qml : http://xizhizhu.blogspot.in/2010/10/...-and-qt-c.html

    Basically you need to set the context property for the same view(for same qml) which is being loaded next. So, going by this, set the context property for MainPage.qml as well before launching it.

  7. #7
    Regular Contributor
    Join Date
    Nov 2011
    Location
    Tunisia
    Posts
    81

    Re: QML & Qt C++ : ReferenceError

    Quote Originally Posted by vineet.jain View Post
    You can refer here as how c++ is exposed from qml : http://xizhizhu.blogspot.in/2010/10/...-and-qt-c.html

    Basically you need to set the context property for the same view(for same qml) which is being loaded next. So, going by this, set the context property for MainPage.qml as well before launching it.
    Hi again Jain,
    How I can set the context property for MainPage.qml as well as for main.qml? I doesn't found how I can do this in the link

  8. #8
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Gurgaon,India
    Posts
    4,101

    Re: QML & Qt C++ : ReferenceError

    When you create the MainPage.qml using let say QDeclarativeComponent , at the same time you need to set the context property for that view which will expose the c++ class to that qml class.

    QDeclarativeEngine engine;
    QDeclarativeComponent component(&engine, QUrl::fromLocalFile("qml/test022/MainPage.qml"));
    QDeclarativeContext *context = engine()->rootContext();
    context->setContextProperty("TestClass", this);
    QObject *object = component.create();

  9. #9
    Regular Contributor
    Join Date
    Nov 2011
    Location
    Tunisia
    Posts
    81

    Thumbs up Re: QML & Qt C++ : ReferenceError [Resolved]

    Thank you again Jain,
    Finally I found the source of the error, I need to added a latency timer to wait the creation of the Component.
    I just changed the MainPage.qml as below and all works without errors now:

    Code:
    import QtQuick 1.1
    import com.nokia.symbian 1.1
    import com.nokia.extras 1.1
    
    Page {
        id: mainPage
    
        function jsFunction(jsVar){
            console.log(jsVar)
            return "003"
        }
    
        Text {
            anchors.centerIn: parent
            text: qsTr("Hello world!")
            color: platformStyle.colorNormalLight
            font.pixelSize: 20
        }
    
        Timer{
            id: timer
            interval: 10
            repeat: false
            running: false
            onTriggered: TestClass.showText("001")
        }
    
        Component.onCompleted: timer.start()
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×