[SailfishDevel] QDeclarativePropertyMap not dynamic?
Ruediger Gad
r.c.g at gmx.de
Wed May 8 20:16:39 UTC 2013
Hi,
I wrote a blog post about exchanging things between QML <-> C++ quite
some time ago:
http://ruedigergad.com/2011/11/13/exchange-data-and-objects-between-c-and-qml-and-vice-versa/
I am not explicitly covering signals there, iirc, but when you define a
Q_PROPERTY in C++ with a defined NOTIFY then you can use the usual QML
way of handling signals: 'onFooChanged: console.log("bar")'. See, e.g.,
C++ class A in the example.
Of course, you can also use simple signals as well. Just remember that
the C++ signal "bar()" will be "onBar" in QML.
Personally, I found it easiest to expose C++ objects to QML via the
qmlRegisterType mechanism as explained in that post.
I always found the approach of setting context properties to be
artificially limiting and not straight forward.
I hope this helps.
Regards
Ruediger
On 05/08/2013 09:27 PM, christopher.lamb at thurweb.ch wrote:
> Hi Superpelican
>
> Why don't you post the QML / UI part of your code, so we can see how you
> think your QML should respond to your QDeclarativePropertyMap?
>
> Does your PropertyMap have an onXXXXChanged event? That would be one QML
> way of allowing UI components to respond to property changes.
>
> You could also try posting this question on the Qt Project Forum - I
> suspect it is more of a Qt question than a SailfishOS specific one, so
> you could leverage all the Qt knowledge there.
>
> Grüsse
>
> Chris
>
>
> Zitat von Superpelican <superpelican at zoho.com>:
>
>> 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
--
http://ruedigergad.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4441 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.sailfishos.org/pipermail/devel/attachments/20130508/bef433fb/attachment.bin>
More information about the Devel
mailing list