[HELP] : access QML object on c++
-
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.15Item{
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
-
First of all,
findChild()
will scan forobjectName: "me"
, not forid: 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
-
@sierdzio said in [HELP] : access QML object on c++:
First of all,
findChild()
will scan forobjectName: "me"
, not forid: 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 :)