QML, QtQuick - modify text object from C++ code
-
I have done a lot of research but I failed to find such simple example. I'm obviously searching wrong thing because I cannot imagine that modifying a GUI text from code behind is very complex.
I have read the "QtBindings page":http://qt-project.org/doc/qt-4.8/qtbinding.html and the example included on this page (shown below) but was unable to apply it to my embedded Qt project which uses QDeclarativeView to interface with LCD.
@ // main.cpp
QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, "MyItem.qml");
QObject *object = component.create();QVariant returnedValue;
QVariant msg = "Hello from C++";
QMetaObject::invokeMethod(object, "myQmlFunction",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, msg));qDebug() << "QML function returned:" << returnedValue.toString();
delete object;@So here is a simplified version of my code. Could someone tell me if it is possible for the QML to detect a change value of the QString variable when a setText() is invoked. I am guessing that I need a connect statement which will bind my textChanged() signal with the QML. I also was not able to find such example.
How would I define a slot in QML and for what object? In which file should I place this connect statement? I am really looking for syntax here...I am also aware of findChild way of locating objects in QML but apparently this is not a good way of doing such task in Qt per the same qt-project page I have mentioned above.
This code works, but the second setText("Change 2") does not update screen. The screen is only updated once when the setContextProperty line is executed.
@// main.cpp
#include <QApplication>
#include "qmlapplicationviewer.h"
#include "mydisplay.h"Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
MyDisplay display;// This setText updates QML display.setText("Change 1"); view.setSource(QUrl::fromLocalFile("qml/qmlTest1/main.qml")); view.rootContext()->setContextProperty("myDisplay", &display); // This setText does not update QML display.setText("Change 2"); // Debug console displayes "Change 2" string correctly. qDebug() << display.getText(); view.show(); return app.exec();
}
@
@//mydisplay.h
#include <QObject>
#include <QtDeclarative>#ifndef MYDISPLAY_H
#define MYDISPLAY_Hclass MyDisplay : public QObject
{
Q_OBJECT
Q_PROPERTY(QString newText READ getText WRITE setText NOTIFY textChanged)public:
MyDisplay();MyDisplay(QString); Q_INVOKABLE QString getText() const;
public slots:
void setText(QString text);signals:
void textChanged(QString);private:
QString newText;
};#endif // MYDISPLAY_H@
@//mydisplay.cpp
#include "mydisplay.h"MyDisplay::MyDisplay()
{
newText = "";
}MyDisplay::MyDisplay(QString text)
{
newText = text;
}QString MyDisplay::getText() const
{
return newText;
}void MyDisplay::setText(QString text)
{
if (text != newText)
{
newText = text;
emit textChanged(text);
}
}@@// main.qml
import QtQuick 1.1Rectangle {
width: 800
height: 480
color: "#000000"Text { id: myQmlTextObject anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter color: "#b9b9b9" font.pixelSize: 30 text: myDisplay.getText(); }
}@
Working code would be greatly appreciated. -
@
// in QML
text: myDisplay.newText
@You need to bind to the property, not it's getter method.