Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ object in QML: "Can't find variable:"

C++ object in QML: "Can't find variable:"

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wspilot
    wrote on last edited by
    #1

    Hi,
    A have a simple progr to tests QtWidget-C++ QML communication.
    QML component shows well , but the C++ object, named "myObject0" for QML is not found:
    "ReferenceError: Can't find variable: myObject0"

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

    MainWindow mainWindow;
    test0 tt0;
    tt0.cppSlot(9999);//works ok
    QDeclarativeEngine* engine = new QDeclarativeEngine;
    QDeclarativeContext *context = new QDeclarativeContext(engine->rootContext());
    context->setContextProperty("myObject0", &tt0);
    
    QDeclarativeComponent component(engine, QUrl::fromLocalFile("../qml1/test0.qml"));//Creates a COMPONENT from the given fileName and give it the specified parent and engine.
    QObject *object = component.create();//Create an object instance from this component
    QGraphicsObject *graphicsObject = qobject_cast<QGraphicsObject *>(object);
    

    qDebug() << component.errors();
    graphicsObject->setProperty("width", 300);//works ok
    Scene0 scene0;
    scene0.setItemIndexMethod(QGraphicsScene::NoIndex);
    mainWindow.grahicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    mainWindow.grahicsView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    mainWindow.grahicsView->setScene(&scene0);
    mainWindow.grahicsView->show();

    QVariant returnedValue;
    QVariant msg = "text";
    QMetaObject::invokeMethod(object, "test0", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, msg));//works ok
    qDebug() << returnedValue.toString();
    

    ...
    }
    @
    test0.qml:
    @Item
    {
    function test0(msg)//works ok
    {
    console.log(msg)
    helloText.text = msg
    }

    Rectangle
    {
    id: rectangle1
    width: 300
    height: 600
    color: "red"
    radius: 14
    Text
    {
    id: helloText
    text: "HW"
    y: 36
    }
    MouseArea
    {
    anchors.fill: parent
    clip: false
    smooth: false
    hoverEnabled: true
    onClicked: {myObject0.cppSlot(123456)}
    }
    }
    }
    @
    test0.h:
    @class test0 : public QObject
    {
    Q_OBJECT
    public:
    explicit test0(QObject *parent = 0);
    public slots:
    void cppSlot(int number);

    };@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      coyotte508
      wrote on last edited by
      #2

      change @QDeclarativeContext *context = new QDeclarativeContext(engine->rootContext());@ to @QDeclarativeContext *context = engine->rootContext();@

      Also try before everything else to add:

      @qmlRegisterType<test0>();@

      Also, it's a good idea to have your class names (here test0) start by uppercase (Test0).

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wspilot
        wrote on last edited by
        #3

        Thanks al lot. You saved my day!
        I took my code from the documentation "QDeclarativeContext Class Reference":http://qt-project.org/doc/qt-4.8/QDeclarativeContext.html.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved