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. [HELP] : access QML object on c++
QtWS25 Last Chance

[HELP] : access QML object on c++

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

    Hi, would like to ask how can i access QML object from within the c++?

    for example

    this is my main.qml
    import QtQuick
    import "global/views/" as Component
    Window {
    id: mainWindow
    width: 480
    height: 800
    visible: true
    title: qsTr("Movie Stream")

    Component.BorderedTextView{
        borderColor: "#000000"
        id: me
        textHint: "sample"
        radius:  5
        width: 200
        height: 50
    
    }
    

    }

    and the BorderedTextView.qml
    import QtQuick 2.15

    Item{
    property double radius:0
    property string textHint: ""
    property color borderColor: "#e8e8e8"
    property color hintColor : "#888888"
    property color textColor: "#000000"
    property double fontSize: 12
    property double borderWidth: 1
    property string text:""

    id: myItem
    Rectangle{
    
        width: myItem.width
        height: myItem.height
        border.color: myItem.borderColor
        border.width:  myItem.borderWidth
        radius: myItem.radius
        clip : true
        TextEdit{
            id:textEdit
            text:myItem.text
            color:  myItem.textColor
            width: myItem.width
            height: myItem.height
            textMargin: 2
            font.pointSize: myItem.fontSize
            Text{
                visible: !textEdit.text
                text: myItem.textHint
                color: myItem.hintColor
                font.pointSize: myItem.fontSize
            }
        }
    }
    

    }

    and this is my main.cpp
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>

    #include <QFileInfo>
    #include <QLocale>
    #include <QTranslator>
    #include <Qt>
    #include <iostream>
    #include <QDebug>
    #include <QQmlContext>
    #include <QWindow>
    #include <QScreen>

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

    QGuiApplication app(argc, argv);
    std::string platform = app.platformName().toStdString();
    
    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "MovieStream_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            app.installTranslator(&translator);
            break;
        }
    }
    
    QQmlApplicationEngine engine;
    const QUrl url(u"qrc:/MovieStream/main.qml"_qs);
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);
    
    qDebug() <<app.allWindows()[0]->findChild<QObject*>("me");
    return app.exec();
    

    }

    but it always return null or QObject(0x0)

    i also try to use engine.rootObjects()[0]->findChild<QObject*>("me"); and engine.rootContext.findChild<QObject*>("me"); but it gives me the same return

    how can i achieve that.

    thanks

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      First of all, findChild() will scan for objectName: "me", not for id: me.

      Second, you should probably search through children of the engine.

      Third: read the docs :-) https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html and https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html

      (Z(:^

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

        Fourth: Don't

        • https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
        • http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
        • https://youtu.be/vzs5VPTf4QQ?t=23m20s
        1 Reply Last reply
        0
        • sierdzioS sierdzio

          First of all, findChild() will scan for objectName: "me", not for id: me.

          Second, you should probably search through children of the engine.

          Third: read the docs :-) https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html and https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html

          J Offline
          J Offline
          jhayar
          wrote on last edited by
          #4

          @sierdzio said in [HELP] : access QML object on c++:

          First of all, findChild() will scan for objectName: "me", not for id: me.

          Second, you should probably search through children of the engine.

          Third: read the docs :-) https://doc.qt.io/qt-6/qtqml-cppintegration-interactqmlfromcpp.html and https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html

          hi , thanks , got it working :) i thought that findChild is looking for id

          will mark this as solved thanks :)

          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