QQuickWidget and C++ interaction
Give it a try:
view->rootContext()->setContextProperty("test", "some random text");
instead of
view->setProperty("test", 0);
setProperty(name, val)
works if object has the property name
defined as Q_PROPERTY
.
It is possible to pass QObject
-derived object as view
's context property:
class Controller : public QObject
{
Q_OBJECT
QString m_test;
public:
explicit Controller(QObject *parent = 0);
Q_PROPERTY(QString test READ test WRITE setTest NOTIFY testChanged)
QDate test() const
{
return m_test;
}
signals:
void testChanged(QString arg);
public slots:
void setTest(QDate arg)
{
if (m_test != arg) {
m_test = arg;
emit testChanged(arg);
}
}
};
Controller c;
view->rootContext()->setContextProperty("controller", &c);
Text {
id: text
width: mainWindow.width
font.pixelSize: 20
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: controller.test
}
Is it also possible to get the Text object in C++ and update its text?
In general, it doesn't seem to be the best approach -- c++
code shouldn't be aware of presentation if it follows model-view pattern.
However it is possible as described here.