[SailfishDevel] QDeclarativePropertyMap not dynamic?

Superpelican superpelican at zoho.com
Wed May 8 09:46:42 UTC 2013


I'm trying to create a hybrid QML/C++ application, where the logic is 
written in C++ and the interface is QML/Sailfish Silica based.

I'm currently playing around with the different ways to let QML/C++ 
communicate with each other. I currently have this code:

<code>
#include <QApplication>
#include <QGraphicsObject>
#include <QDir>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDebug>
#include <QDeclarativePropertyMap>

#ifdef HAS_BOOSTER
#include <MDeclarativeCache>
#endif

Q_DECL_EXPORT int main(int argc, char *argv[])
{
     #ifdef HAS_BOOSTER
         QScopedPointer<QApplication> 
myapp(MDeclarativeCache::qApplication(argc, argv));
     #else
         QScopedPointer<QApplication> myapp = new QApplication(argc, argv);
     #endif


     #ifdef HAS_BOOSTER
         QScopedPointer<QDeclarativeView> 
appview(MDeclarativeCache::qDeclarativeView());
     #else
         QScopedPointer<QDeclarativeView>(new QDeclarativeView);
     #endif

     QDeclarativePropertyMap binding_map;
     binding_map.insert("question_txt", QVariant(QString("5 * 5 =")));
     binding_map.insert("color", QVariant(QString("dark red")));
     QScopedPointer<QDeclarativeContext> 
binding_context(appview->rootContext());
     binding_context->setContextProperty("binding_map", &binding_map);
     QString file = "main.qml";
     QString path = QString(DEPLOYMENT_PATH);
     appview->setSource(QUrl::fromLocalFile(path + file));
appview->setResizeMode(QDeclarativeView::SizeRootObjectToView);
     appview->setAttribute(Qt::WA_OpaquePaintEvent);
     appview->setAttribute(Qt::WA_NoSystemBackground);
appview->viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
appview->viewport()->setAttribute(Qt::WA_NoSystemBackground);
     appview->showFullScreen();
     binding_map["question_txt"] = QVariant(QString("overwritten 5 * 5 ="));
     return myapp->exec();
}
</code>
I need to let C++ print text in the QML interface (the question) and C++ 
has to obtain the
answer the user has answered in the QML interface (TextField Silica 
component). That's basically
the needed communication between C++ and QML.

So I thought that I'd create a QDeclarativePropertyMap in C++. This 
propertymap will contain the question.
My program has a while loop that asks the user new questions each time 
the loop runs(the logic code can be found here 
<https://bitbucket.org/Superpelican/clamshell_cli>,
but it hasn't been adjusted for use with a GUI, it's currently a CLI 
application). So I need to constantly update the QML UI from C++
while the programs running, after I've setup the QDeclarativeView etc.

However I noticed that if you change a value in the propertymap after 
initializing and showing the QDeclarativeView, the UI won't be updated!
I thought the QDeclarativePropertyMap was dynamic! 
http://qt-project.org/doc/qt-4.8/qdeclarativepropertymap.html#details: 
"The binding is dynamic - whenever a key's value is updated, anything 
bound to that key will be updated as well."

Kind Regards,

Superpelican


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sailfishos.org/pipermail/devel/attachments/20130508/9ab19d13/attachment.html>


More information about the Devel mailing list