Forums

OverviewFelgo 2 Support (Qt 5) › Error with VPlayCppQML-Example

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #20327

    Gabor

    Hi,

    I started to experiment with this example. Unfortunately I keep getting the following error message:

    ReferenceError: myGlobalObject is not defined.

    myglobalobject.h:

    #ifndef MYGLOBALOBJECT_H
    #define MYGLOBALOBJECT_H
    
    #include <QObject>
    
    class MyGlobalObject : public QObject
    {
        Q_OBJECT
    
    public:
        MyGlobalObject();
    
    public slots:
        void doSomething(const QString &text);
    };
    
    #endif // MYGLOBALOBJECT_H
    

    main.cpp:

    #include <QApplication>
    #include <VPApplication>
    
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "myglobalobject.h"
    
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        VPApplication vplay;
    
        vplay.setPreservePlatformFonts(true);
    
        QQmlApplicationEngine engine;
        vplay.initialize(&engine);
    
        vplay.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
    
        MyGlobalObject *myGlobal = new MyGlobalObject();
        myGlobal->doSomething ("TEXT FROM C++");
        engine.rootContext ()->setContextProperty ("myGlobalObject",myGlobal);
    
        engine.load(QUrl(vplay.mainQmlFileName()));
    
        return app.exec();
    }
    

    myglobalobject.cpp:

    #include "myglobalobject.h"
    #include <QDebug>
    
    MyGlobalObject::MyGlobalObject()
    {
    
    }
    
    void MyGlobalObject::doSomething(const QString &text)
    {
    qDebug() << "MyGlobelObject doSomething called with " << text;
    }
    

    main.qml:

    import Felgo 3.0
    import QtQuick 2.0
    
    App {
    
        NavigationStack {
    
            Page {
                title: qsTr("Integration")
    
                Column {
    
                    AppButton {
                        text: "MyGlobalObject.doSomething()"
                        onClicked: myGlobalObject.doSomething("TEXT FROM QML")
                    }
                }
            }
        }
    }
    

    Please tell me what is wrong with my code.

    Thank you for your help.

    #20331

    Günther
    Felgo Team

    Hi,

    are you manually building the project with the green play button in Qt Creator?

    As this example uses C++ code, which require compilation, these code parts are not available when using the Felgo Live Preview on Desktop or Mobiles.

    Best,
    Günther

    #20336

    Gabor Toth

    Hi  Günther,

    I tried both ways and I even cleaned, ran qmake and rebuilt the project. I kept getting the same error message. I am handbuilding it to learn.

    When I downloaded the sample and ran it it was looking for a company file in c++. How can I create that company file?

    Thank you.

     

    #20341

    Günther
    Felgo Team

    Hi,

    did you make any changes that could affect the demo behavior, or do you run into this issue directly with the demo taken from GitHub?

     

    Regarding the myGlobalObject identifier: In your main.cpp, this line adds the myGlobalObject context property:

        engine.rootContext ()->setContextProperty ("myGlobalObject",myGlobal);
    

    Thus, you should be able to to use the myGlobalObject identifier in your QML code.

    You can also find a written guide about the Felgo CPP Example here: https://felgo.com/cross-platform-development/how-to-expose-a-qt-cpp-class-with-signals-and-slots-to-qml

     

    Hope this helps,
    Günther from Felgo

    #20343

    Gabor Toth

    Hi,

    I ran into the issue with the downloaded example. I reinstalled Qt and that solved the issue. Thank you for your help.

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support