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


_______________________________________________
SailfishOS.org Devel mailing list

Reply via email to