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 doesn't work
Forum Updated to NodeBB v4.3 + New Features

->setContextProperty doesn't work

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qqmlapplication
5 Posts 4 Posters 1.8k 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.
  • Q Offline
    Q Offline
    Quentin91
    wrote on last edited by Quentin91
    #1

    Hi !

    I'm trying to send a QString to my QML code to print it on a window but I can't manage to use the QQmlApplicationEngine to set a context property.

    Here's my code :

    string tmpStr = testParams(); //returns a std::string. It works, np
    char data[15];
    int i= -1;
    while((data[++i]!='\0')data[i]=tmpStr[i];/ /I know but fromStdString() did not work, I don't know why.
    QString qstr(data);
    
    QGuiApplication app(ai_argc, ai_argv);
    QQmlApplicationEngine engine;
    engine.addImportPath(QStringLiteral("../etc/qml"));
    
    engine.rootContext()->setContextProperty("testStr",testQStr); //here is said "use of an undefined type 'QQmlType'" and
    //Error C2027 :  "Pointer to an incomplete class forbidden"
    
    engine.load(QUrl(QML_TEST1_PATH));
    
    return app.exec();
    

    I think I have to initialize engine, but this using of QQmlApplicationContext is not mine, so I guess that the problem is somewhere else or maybe it's because I use the version 5.12.0 of Qt ? How can I get my program to work and print the string read from my config file in the window ?

    At the end I would like to be able to send information from QML to C++ too.

    Thanks in advance

    J.HilkJ 1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      Add #include <QQmlContext>

      1 Reply Last reply
      3
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by dheerendra
        #3

        You are not calling the method rootContext(). Change like follows.

        engine.rootContext()->setContextProperty("testStr",testQStr);

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

        1 Reply Last reply
        3
        • Q Quentin91

          Hi !

          I'm trying to send a QString to my QML code to print it on a window but I can't manage to use the QQmlApplicationEngine to set a context property.

          Here's my code :

          string tmpStr = testParams(); //returns a std::string. It works, np
          char data[15];
          int i= -1;
          while((data[++i]!='\0')data[i]=tmpStr[i];/ /I know but fromStdString() did not work, I don't know why.
          QString qstr(data);
          
          QGuiApplication app(ai_argc, ai_argv);
          QQmlApplicationEngine engine;
          engine.addImportPath(QStringLiteral("../etc/qml"));
          
          engine.rootContext()->setContextProperty("testStr",testQStr); //here is said "use of an undefined type 'QQmlType'" and
          //Error C2027 :  "Pointer to an incomplete class forbidden"
          
          engine.load(QUrl(QML_TEST1_PATH));
          
          return app.exec();
          

          I think I have to initialize engine, but this using of QQmlApplicationContext is not mine, so I guess that the problem is somewhere else or maybe it's because I use the version 5.12.0 of Qt ? How can I get my program to work and print the string read from my config file in the window ?

          At the end I would like to be able to send information from QML to C++ too.

          Thanks in advance

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Quentin91 to add what was already said,
          Where is testQStr defined? I do Not see it in your code example


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          Q 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @Quentin91 to add what was already said,
            Where is testQStr defined? I do Not see it in your code example

            Q Offline
            Q Offline
            Quentin91
            wrote on last edited by
            #5

            @J.Hilk I think it was just an error while re-writing my code. My dev computer has no internet connection. I was supposed to use qstr

            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