Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change QML object properties from C++.
Forum Updated to NodeBB v4.3 + New Features

Change QML object properties from C++.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 883 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
    Homer JS
    wrote on last edited by
    #1

    I'm trying to do a simple task as changing a property (text: ) of some QML object from C++ yet I'm failing miserably. Any help appreciated.

    I'm not getting any errors, the window shows up, just the text property doesn't change as (at least I think) it s hould. Is even anything I'm NOT doing wrong here?!!

    What I was trying is this:

    main.cpp
    @#include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQuickView>
    #include <QQuickItem>
    #include <QQmlEngine>
    #include <QQmlComponent>
    #include <QString>

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

    QQmlApplicationEngine engine;
    
    
    QQmlComponent component(&engine, QUrl::fromLocalFile&#40;"main.qml"&#41;);
    QObject *object = component.create();
    
     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    QString thisString = "Dr. Perry Cox";
    
    object->setProperty("text", thisString);  //<--- tried  instead of thisString putting "Dr. ..." but nope.
    delete object;
    
    
    
    return app.exec();
    

    }@

    main.qml
    @
    import QtQuick 2.2
    import QtQuick.Window 2.1

    Window {
    visible: true
    width: 360
    height: 360

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    
    Text {
        id: whot
        text: ""
        anchors.centerIn: parent
        font.pixelSize: 20
        color: "green"
    }
    

    }@

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vtong
      wrote on last edited by
      #2

      Hi,
      I think you can try this
      @
      QQmlApplicationEngine engine;
      QQmlContext *context = engine.rootContext();
      QString thisString = "Dr. Perry Cox";
      context->setContextProperty("myText", thisString);
      @

      Hope this help

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Homer JS
        wrote on last edited by
        #3

        Thank you for the answer, very much appreciated. Someone helped me earlier on another website though. Good luck.

        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