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. Context property works in QQmlApplicationEngine but not in QQmlEngine.
Qt 6.11 is out! See what's new in the release blog

Context property works in QQmlApplicationEngine but not in QQmlEngine.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 3.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    adutzu89
    wrote on last edited by adutzu89
    #1

    Hello,
    Because I cannot save/load position of ApplicationWindow correctly, on X11 I have decided to use QMainWindow as base window application but I am stuck with context properties.
    Here you can see how I set the context properties in my main.cpp file.

    In the following is how I declared in QMainWindow constructor:

        QQuickWidget *view = new QQuickWidget();
        QQmlEngine *engine = view->engine();
        connect(engine, SIGNAL(quit()), this, SLOT(close()));
        QQmlContext *context = engine->rootContext();
        Util util;
        SettingsController settingsController;
        engine->addImageProvider(QLatin1String("customimage"), new CustomImageProvider());
        engine->addImageProvider(QLatin1String("fontimage"), new FontImageProvider());
        context->setContextProperty("util", &util);
        context->setContextProperty("settingsController", &settingsController);
        context->setContextProperty("applicationPath", "file://" + qApp->applicationDirPath() + "/");
        registerQmlType();
        if (Util::osType() == "android")
            view->setSource(QUrl(QLatin1String("qrc:/main-android.qml")));
        else
            view->setSource(QUrl(QLatin1String("qrc:/main-desktop.qml")));
    
        if (QFile::exists(QApplication::applicationDirPath() + "/maintenancetool")) {
            QThread thread;
            ThreadWorker *threadWorker = new ThreadWorker();
            threadWorker->moveToThread(&thread);
            QObject::connect(&thread, SIGNAL(finished()), threadWorker, SLOT(deleteLater()));
            QObject::connect(threadWorker, SIGNAL(updateSearchFinished()), &thread, SLOT(quit()));
            thread.start();
            threadWorker->updaterTimerStart();
        }
        view->show();
        view->setResizeMode(QQuickWidget::SizeRootObjectToView);
        view->setClearColor(Qt::transparent);
        setAttribute(Qt::WA_TranslucentBackground);
        loadSettings();
        setCentralWidget(view);
        setMinimumHeight(150);
        setMinimumWidth(140);
    

    My issue is that when I call method of object declared as context property("util" in my example) I get TypeError: Cannot call method '<any-method-here>' of null, while "applicationPath" property works correctly.

    Methods are marked as Q_INVOKABLE.

    Any ideeas on what am I doing wrong?

    E 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      U r creating stack objects in constructor. All these objects are destroyed once the constructor is over. Create dynamic object & things should work.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      A 1 Reply Last reply
      3
      • A adutzu89

        Hello,
        Because I cannot save/load position of ApplicationWindow correctly, on X11 I have decided to use QMainWindow as base window application but I am stuck with context properties.
        Here you can see how I set the context properties in my main.cpp file.

        In the following is how I declared in QMainWindow constructor:

            QQuickWidget *view = new QQuickWidget();
            QQmlEngine *engine = view->engine();
            connect(engine, SIGNAL(quit()), this, SLOT(close()));
            QQmlContext *context = engine->rootContext();
            Util util;
            SettingsController settingsController;
            engine->addImageProvider(QLatin1String("customimage"), new CustomImageProvider());
            engine->addImageProvider(QLatin1String("fontimage"), new FontImageProvider());
            context->setContextProperty("util", &util);
            context->setContextProperty("settingsController", &settingsController);
            context->setContextProperty("applicationPath", "file://" + qApp->applicationDirPath() + "/");
            registerQmlType();
            if (Util::osType() == "android")
                view->setSource(QUrl(QLatin1String("qrc:/main-android.qml")));
            else
                view->setSource(QUrl(QLatin1String("qrc:/main-desktop.qml")));
        
            if (QFile::exists(QApplication::applicationDirPath() + "/maintenancetool")) {
                QThread thread;
                ThreadWorker *threadWorker = new ThreadWorker();
                threadWorker->moveToThread(&thread);
                QObject::connect(&thread, SIGNAL(finished()), threadWorker, SLOT(deleteLater()));
                QObject::connect(threadWorker, SIGNAL(updateSearchFinished()), &thread, SLOT(quit()));
                thread.start();
                threadWorker->updaterTimerStart();
            }
            view->show();
            view->setResizeMode(QQuickWidget::SizeRootObjectToView);
            view->setClearColor(Qt::transparent);
            setAttribute(Qt::WA_TranslucentBackground);
            loadSettings();
            setCentralWidget(view);
            setMinimumHeight(150);
            setMinimumWidth(140);
        

        My issue is that when I call method of object declared as context property("util" in my example) I get TypeError: Cannot call method '<any-method-here>' of null, while "applicationPath" property works correctly.

        Methods are marked as Q_INVOKABLE.

        Any ideeas on what am I doing wrong?

        E Offline
        E Offline
        Eeli K
        wrote on last edited by Eeli K
        #3

        @adutzu89
        @dheerendra is correct. The string works because it's not a pointer or reference but is copied and becomes a javascript string by automatic C++/QML type conversion. However, I don't know what dheerendra means by "dynamic", maybe that they can be heap objects; you can have the objects also as direct members of your main window class without pointers and new. That would be better modern C++ style unless you need to reach the parent main window object from your objects.

        1 Reply Last reply
        2
        • dheerendraD dheerendra

          U r creating stack objects in constructor. All these objects are destroyed once the constructor is over. Create dynamic object & things should work.

          A Offline
          A Offline
          adutzu89
          wrote on last edited by
          #4

          @dheerendra and @Eeli-K thank you for your answers it helped :).

          @Eeli-K he reffers to instantiate the object with the "new" operator.
          In my constructor, as an example:

          Util *util = new Util();
          

          works correctly.

          1 Reply Last reply
          1

          • Login

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