Cannot modify Qml propriety from Cpp
-
Hi, thanks for the answer.
I've changed the code:
@int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv); //preparo l'applicazione
QQmlApplicationEngine engine(QUrl("qml/main.qml")); //carico l'interfaccia
QObject *radice = engine.rootObjects().value(0); //identifico la radice
QQuickWindow *applicationwindow = qobject_cast<QQuickWindow *>(radice); //acquisisco in C++ l'ogg QML ApplicationWindow
if ( !applicationwindow )
{
qWarning("Error: Your root item has to be a Window.");
return -1;
}
applicationwindow->show();
return app.exec();QObject *button = radice->findChild<QObject*>("buttonOpenCloseCom",Qt::FindChildrenRecursively); if (button) { button->setProperty("text", "Open"); qDebug()<<"found!"; } else { button->setProperty("text", "Open"); qDebug()<<"not found!"; }
}@
but there is no output.
The problem persists!I've also tried to change the buttonComText in ApplicationWindows, but the result is the same.
-
Try this:
objectName: "buttonOpenCloseCom" -
There is yet the objectName proprety.
I've tried this solution... but doesn't work.
[quote author="wspilot" date="1376930837"]Try this:
objectName: "buttonOpenCloseCom"[/quote] -
In your code it is still an identifier/var or whatever, but not a string
objectName: buttonOpenCloseCom
not
objectName: “buttonOpenCloseCom” -
Excuseme but in the code:
@
Button
{
id: buttonOpenCloseCom
objectName: buttonOpenCloseCom
text:applicationwindow.buttonComText
enabled: true
}
@objectName: “buttonOpenCloseCom” there is![/quote]
what is the problem you talk about?!
-
Put it in quotes: ". Not just string but "string".
-
@ Button
{
id: buttonOpenCloseCom
objectName: "buttonOpenCloseCom"
text:applicationwindow.buttonComText
enabled: true
}@ -
I've understand what you mean, and I've tried with the correction, but the result is the same as before.
Debugging i've see that the root object is correctly the "applicationwindow",
but when I try to find the child, it fails and return a null pointer...
in "this":http://qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.html#loading-qml-objects-from-c reference it used QQuickView, and not QQuickWindow to handle the root object...
Could it be a problem?
If yes, how can I resolve it? any suggestions? -
The problem is that you make the change after the return() statement.
Nothing after that will be executed. -
I've modified the code:
@int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv); //preparo l'applicazione
QQmlApplicationEngine engine(QUrl("qml/main.qml")); //carico l'interfaccia
QObject *radice = engine.rootObjects().value(0); //identifico la radice
QQuickWindow *applicationwindow = qobject_cast<QQuickWindow *>(radice); //acquisisco in C++ l'ogg QML ApplicationWindow
if ( !applicationwindow )
{
qWarning("Error: Your root item has to be a Window.");
return -1;
}
QObject button = radice->findChild<QObject>("buttonOpenCloseCom",Qt::FindChildrenRecursively);
if (button)
{
button->setProperty("text", "Open");
qDebug()<<"found!";
}
else
{
button->setProperty("text", "Open");
qDebug()<<"not found!";
}applicationwindow->show(); return app.exec();
}@
but is the same as before...
[quote author="mranger90" date="1376947874"]The problem is that you make the change after the return() statement.
Nothing after that will be executed.[/quote] -
One drawback of QML is that it's mightily hard to explain certain things... In order for this to work (IMO) you need to do that property manipulation outside of "main.cpp":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfmain.cpp so for example in a QObject that gets initialised somewhere later. Same as with QWidgets or console apps in Qt: you need to have a working event loop for moc to work.
One possible alternative would be to modify the property using Qt::QueuedConnection, or the infamous singleshot QTimer.