Changing property in qml file from c++ file
-
Here is my main.cpp file,
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <QQmlComponent> #include "datastore.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QQmlContext* context = engine.rootContext(); DataStore dt; context->setContextProperty("dataStore", &dt); QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml"))); QObject *object = component.create(); object->setProperty("rootId.width",300); delete object; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } and here is my main.qml file,import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4Window {
id: rootId
visible: true
width: 640
height: 480
title: qsTr("Hello World")onWindowStateChanged: console.log("The width is",width) property string textToShow: "" Row{ id: row1 anchors.centerIn: parent spacing: 20 Rectangle{ id: redRectId width: 150; height: 150 color: "red" radius: 20 MouseArea{ anchors.fill: parent onClicked: { console.log("Clicked on the red rectangle") } } } Rectangle{ id: greenRectId width: 150; height: 150 color: "green" radius: 20 MouseArea{ anchors.fill: parent onClicked: { console.log("Clicked on the red rectangle") } } } Rectangle{ id: blueRectId width: 150; height: 150 color: "blue" radius: 20 MouseArea{ anchors.fill: parent onClicked: { console.log("Clicked on the red rectangle") } } } }// Button
// {
// id:btnUpdate
// text: "Just Click Me"
// onClicked:dataStore.callMeFromQml()
// }
}And iam unable to set change the width of the window property from my main.cpp file. How can i do it? Thanks in advance. -
Here is my main.cpp file,
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <QQmlComponent> #include "datastore.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QQmlContext* context = engine.rootContext(); DataStore dt; context->setContextProperty("dataStore", &dt); QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml"))); QObject *object = component.create(); object->setProperty("rootId.width",300); delete object; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } and here is my main.qml file,import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4Window {
id: rootId
visible: true
width: 640
height: 480
title: qsTr("Hello World")onWindowStateChanged: console.log("The width is",width) property string textToShow: "" Row{ id: row1 anchors.centerIn: parent spacing: 20 Rectangle{ id: redRectId width: 150; height: 150 color: "red" radius: 20 MouseArea{ anchors.fill: parent onClicked: { console.log("Clicked on the red rectangle") } } } Rectangle{ id: greenRectId width: 150; height: 150 color: "green" radius: 20 MouseArea{ anchors.fill: parent onClicked: { console.log("Clicked on the red rectangle") } } } Rectangle{ id: blueRectId width: 150; height: 150 color: "blue" radius: 20 MouseArea{ anchors.fill: parent onClicked: { console.log("Clicked on the red rectangle") } } } }// Button
// {
// id:btnUpdate
// text: "Just Click Me"
// onClicked:dataStore.callMeFromQml()
// }
}And iam unable to set change the width of the window property from my main.cpp file. How can i do it? Thanks in advance.@JennyAug13 said in Changing property in qml file from c++ file:
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
QObject *object = component.create();
object->setProperty("rootId.width",300);
delete object;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));why?
first you create an instance from a component definition, set a property and delete the instance again.
Then in the next line you create another (new independent) instance.So your first property change goes into nirvana.
-
Oh I am a beginner in this field and how can i do it in right way? I want to make the window width to 300. Thanks in advance.
-
Oh I am a beginner in this field and how can i do it in right way? I want to make the window width to 300. Thanks in advance.
@JennyAug13
one possiblity:engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; QQmlProperty::write(engine.rootObjects().first(), "width", QVariant::fromValue<qreal>(300)); // or use setProperty() -
Yes, it worked like above said way.
-
How can i use setProperty() and do it?
-
How can i use setProperty() and do it?
@JennyAug13
replaceQQmlProperty::write(engine.rootObjects().first(), "width", QVariant::fromValue<qreal>(300));with
engine.rootObjects().first()->setProperty( "width", QVariant::fromValue<qreal>(300) ); -
@JennyAug13
replaceQQmlProperty::write(engine.rootObjects().first(), "width", QVariant::fromValue<qreal>(300));with
engine.rootObjects().first()->setProperty( "width", QVariant::fromValue<qreal>(300) );@raven-worx In the similar way i have a c++ file for setting the pulse width and as the pulse width is changed in the c++ file, corresponding blur radius of the button has to be changed. How can I do it? It is not having engine.rootObjects and it is not my main.cpp file.
Here is the code i hav written but I can see nothing has been done.
QQuickView view; view.setSource(QUrl(QStringLiteral("qrc:/QML/Components/NTActionButton.qml"))); view.rootContext()->setContextProperty("actionButton.shadowBlurRadius",3); qCDebug(LC_NTUserSettingsModel())<< "pulseWidth is changed"<<"button radius is changed";But i can see there is no change in the blur radius of the button. It would be great if someone helps me in finding right way to change my qml property from c++ file, thanks in advance.
-
@raven-worx In the similar way i have a c++ file for setting the pulse width and as the pulse width is changed in the c++ file, corresponding blur radius of the button has to be changed. How can I do it? It is not having engine.rootObjects and it is not my main.cpp file.
Here is the code i hav written but I can see nothing has been done.
QQuickView view; view.setSource(QUrl(QStringLiteral("qrc:/QML/Components/NTActionButton.qml"))); view.rootContext()->setContextProperty("actionButton.shadowBlurRadius",3); qCDebug(LC_NTUserSettingsModel())<< "pulseWidth is changed"<<"button radius is changed";But i can see there is no change in the blur radius of the button. It would be great if someone helps me in finding right way to change my qml property from c++ file, thanks in advance.
@JennyAug13 said in Changing property in qml file from c++ file:
view.rootContext()->setContextProperty("actionButton.shadowBlurRadius",3);
setContextProperty is the incorrect method here. A property name such as
actionButton.shadowBlurRadiusis also not possible.You need an instance to
actionButtonand call setProperty() on it (just like you did before).One way can be to expose the object via a property:
Window { id: rootId property alias actionButton: actionButtonId Item { id: actionButtonId ... }Then you can do
engine.rootObjects().first()->property("actionButton").value<QQuickItem*>()->setProperty("shadowBlurRadius", 3); -
I tried like in the following way, since I am trying to modify my qml file from another c++ source file unlike from the main file. It works fine but it is showing following message
void NTUserSettingsModel::adjustIntensityForPulseWidth() { qDebug("@@@"); QQmlEngine engine; qDebug("@@@"); QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml"))); qDebug("@@@"); QObject *object = component.create(); qDebug("@@@"); object->setProperty("currentIntensityAAA",QVariant(0)); qDebug("@@@"); }And my qml file has following changes:
Component.onCompleted: { currentIntensity = currentIntensityAAA console.log("@@@@@@") console.log(currentIntensity) console.log("@@@@@@") }The error which has been shown when i change my pulseWidth is
[default]: qrc:/QML/Components/NTIntensityControl.qml:188: ReferenceError: currentIntensityAAA is not definedShould i pass the value to my currentIntensityAAA as global in my cpp source file?
-
Swap the statements. It should work.
object->setProperty("currentIntensityAAA",QVariant(0)); QObject *object = component.create(); -
When i swapped it shows an error called use of undeclared identifier 'object' beside ```
object->setProperty("currentIntensity",QVariant(0)); -
When i try the other way, the program unexpectedly finishes.
view->rootContext()->setContextProperty(QStringLiteral("currentIntensityAAA"),QVariant(0)); view->setSource(QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml"))); -
I tried like in the following way, since I am trying to modify my qml file from another c++ source file unlike from the main file. It works fine but it is showing following message
void NTUserSettingsModel::adjustIntensityForPulseWidth() { qDebug("@@@"); QQmlEngine engine; qDebug("@@@"); QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml"))); qDebug("@@@"); QObject *object = component.create(); qDebug("@@@"); object->setProperty("currentIntensityAAA",QVariant(0)); qDebug("@@@"); }And my qml file has following changes:
Component.onCompleted: { currentIntensity = currentIntensityAAA console.log("@@@@@@") console.log(currentIntensity) console.log("@@@@@@") }The error which has been shown when i change my pulseWidth is
[default]: qrc:/QML/Components/NTIntensityControl.qml:188: ReferenceError: currentIntensityAAA is not definedShould i pass the value to my currentIntensityAAA as global in my cpp source file?
@JennyAug13 said in Changing property in qml file from c++ file:
void NTUserSettingsModel::adjustIntensityForPulseWidth()
{
qDebug("@@@");
QQmlEngine engine;
qDebug("@@@");
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/QML/Components/NTIntensityControl.qml")));
qDebug("@@@");
QObject *object = component.create();
qDebug("@@@");
object->setProperty("currentIntensityAAA",QVariant(0));
qDebug("@@@");
}i am not sure if you quite understand some C++ basics yet.
Again - like already mentioned before - you are creating a QQmlEngine on the stack => the QQmlEngine instance will be deleted once you exit the method. Also you are not supposed to create objects from components just when you want to set a property of a specific object.
Thus everything you do in this method might do something, but only in a temporary UI.