Control/update the Qt Quick QML text element from C++
-
Seems like a very common task, yet I was not able to locate a simple example. I come from the Visual C environment were updating a text element is as simple as Text1.String = "My text string";
Unfortunately the Qt/Qt Quick does not offer such a simple solution or does it?My goal is to have a dynamic (run time) text updates based on the activity within the app and Timer events (I got timer working already).
Thank you for any suggestions. Complete examples are greatly appreciated since I am very new to Qt.
-
The thing is that you dont usually set the value of text elements directly in c++. Instead, you declare a string property, modify that and bind your text property to that on the qml side.
This link contains a complete example:
http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.htmlIn particular notice how the author property is exposed on the message object and how it is bound to the text element in QML.
That said it is certainly possible (and should perhaps be more convenient) to obtain a pointer to the text object in C++ and call setProperty directly on it, but it is generally better to keep the UI layer separate.
-
Sorry for a long delay... Thx, this is very good example but I am having hard time getting it to run.
I use Qt 4.8 and not 5.0 and I do not get QQmlEngine (unless there is a specific #include which I am missing)
I tried the following, but do not get anything on screen
I only get this output:
Qml debugging is enabled. Only use this in a safe environment!
QDeclarativeComponent: Component is not ready
@ QCoreApplication app(argc,argv);QDeclarativeEngine engine; Display displayTime; engine.rootContext()->setContextProperty("displayTime", &displayTime); QDeclarativeComponent component(&engine, QUrl::fromLocalFile("qml/QtPlayer3/main.qml")); component.create();
return app.exec();@
Finally do you know the difference between:
QDeclarativeEngine
QQmlEngine
QmlApplicationViewer - this is the only way I am getting output on the screen. Here is the code:@ QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
Display displayTime;
viewer.rootContext()->setContextProperty("displayTime", &displayTime);
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/QtPlayer3/main.qml"));
viewer.showExpanded();
return app->exec();@ -
Hi,
Qt 4.8 uses Qt Quick 1, while Qt 5 uses Qt Quick 2.
In Qt Quick 1, all C++ class names started with QDeclarative*. In Qt Quick 2, those classes have been renamed to QQuick* or QQml* for better modularization, but they still do roughly the same jobs. See "here":http://qt-project.org/doc/qt-5.1/qtdoc/qtquick-porting-qt5.html#c-code for more details.
Anyway, you can find examples for Qt 4.8 at http://qt-project.org/doc/qt-4.8/qtbinding.html
-
Thank you,
I did this exactly as you described(used QDeclarativeEngine.) (shown in the first code example) but get no screen activity. I think the target board crashes because I loose link with QtCreator in this mode.When configured with QmlApplicationViewer instead of QDeclarativeEngine then GUI works fine but Signals do not seem to work.
I am also not 100% sure that I have configured the signals and slots correctly.
Where should the connect statement reside? main.cpp -
Hi,
[quote author="marcin100" date="1378562239"]...get no screen activity. I think the target board crashes because I loose link with QtCreator in this mode.[/quote]I recommend ensuring that the program works on your host PC first, before trying to deploy it to an embedded device. What board is it?
[quote]When configured with QmlApplicationViewer instead of QDeclarativeEngine then GUI works fine but Signals do not seem to work.
I am also not 100% sure that I have configured the signals and slots correctly.
Where should the connect statement reside? main.cpp[/quote]Do you mean QML signals or C++ signals? Connected to QML slots or C++ slots?Please show us your code; it will make it easier to see what you're trying and to spot errors.
-
I think I will stick to QmlApplicationViewer unless someone will tell me this is not a right way.
Board: i.mx6 by Boundary Devices
I did try runing it on PC with same result.
I am almost certain that I need to connect the GUI text object with the MyDisplay object in C++ code, however I am stuck with syntax. I am also not sure in which part of the code should the "connect" statement be placed.
Here is the main.cpp. The displayTime.setText("XXXXXXXX"); works correctly because the next line spits out the right output on the debug console. However the QML text object is still blank.
@int main(int argc, char * argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
MyPlayer player(&viewer);
MyDisplay displayTime;
player.setDisplay(&displayTime);viewer.rootContext()->setContextProperty("myQmlTest", &player); viewer.rootContext()->setContextProperty("displayTime", &displayTime); viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/QtPlayer3/main.qml")); viewer.showExpanded(); viewer.showFullScreen(); player.myGSTint(); // init GStreamer displayTime.setText("XXXXXXXX"); qDebug() << "This is a test: " << displayTime.getText(); return app->exec();
}@
...and the QML section
@
Text
{
id: time
//text: "none"
color: "#999999"
font.pixelSize: 30
anchors.topMargin: 90
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
text: displayTime.getText()
}@and the MyDisplay class
@MyDisplay::MyDisplay()
{
newText = "";
}MyDisplay::MyDisplay(QString text)
{
newText = text;
}QString MyDisplay::getText() const
{
return newText;
}void MyDisplay::setText(const QString &text)
{
if (text != newText)
{
newText = text;
emit textChanged(text);
}
}@