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. setContextProperty "TypeError: Cannot call method"
Forum Updated to NodeBB v4.3 + New Features

setContextProperty "TypeError: Cannot call method"

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 86 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.
  • H Offline
    H Offline
    Heisenberg222
    wrote last edited by
    #1

    Code1

    DataManager* pDataManager = new DataManager;
    engine.rootContext()->setContextProperty("dataManager", pDataManager);
    

    Code2

    DataManager dataManager;
    engine.rootContext()->setContextProperty("dataManager", &dataManager);
    

    DataManager has a method posNum()
    When I run the qml project, Code1 works good,Code2 shows an error "TypeError: Cannot call method 'posNum' of null".
    I don't know the difference between Code1 and Code2.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      What are the lifetime of these objects ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        What are the lifetime of these objects ?

        H Offline
        H Offline
        Heisenberg222
        wrote last edited by
        #3

        @SGaist simple code in main.cpp

        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
        
            QQuickStyle::setStyle("Basic");
            QQmlApplicationEngine engine;
            QObject::connect(
                &engine,
                &QQmlApplicationEngine::objectCreationFailed,
                &app,
                []() { QCoreApplication::exit(-1); },
                Qt::QueuedConnection);
        
            DataManager dataManager;
            engine.rootContext()->setContextProperty("dataManager", &dataManager);
        
            //DataManager* pDataManager = new DataManager;
            //engine.rootContext()->setContextProperty("dataManager", pDataManager);
        
            engine.loadFromModule("Project", "Main");
        
            return app.exec();
        }
        
        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