From kde-devel Mon Jun 06 14:29:26 2011 From: David Edmundson Date: Mon, 06 Jun 2011 14:29:26 +0000 To: kde-devel Subject: Re: Communication between C++ and QML Message-Id: X-MARC-Message: https://marc.info/?l=kde-devel&m=130737063401649 On Fri, Jun 3, 2011 at 10:12 PM, Stefan Majewsky wrote: > Hi, > > I've been looking into QML again for kdegames. Some background: I > added a QDeclarativeImageProvider to feed images from libtagaro's > ThemeProvider into QML, and that mostly works (with an emphasis on > mostly; QTBUG-14900). Then I tried something more difficult: I wanted > to port a game from QML, and chose KNetwalk because it has its > algorithms split in an AbstractGrid class, which is subclassed for the > QGraphicsView representation. > > Now as far as I understand the (IMHO surprisingly sparse) > QtDeclarative module documentation, the main gateway for transferring > information between C++ and QML is > declarativeView->engine()->rootContext()->setContextObject(). QML > retrieves data from the properties of this object and the subobjects > which it references, and invokes via JavaScript functions in this > object to propagate user input to the C++ layer. > > Is this correct? If so, I get a feeling that it would require a > potentially epic amount of boilerplate to make my already > interface-agnostic data available to QML. Can I do anything to make > this task simpler? > That's not the only way to link C++ and QML. You can also make your QObject creatable during the QML runtime. It /may/ be easier for your purposes. In your application that creates the QDeclarativeView run (for example) qmlRegisterType("KDEGames", 0, 1, "HighScoreManager"); Then in your qml files import KDEGames 0.1 HighScoreManager{ id: manager; //whatever else. } It should be mostly be a case of adding a few Q_PROPERTY and Q_INVOKABLE macros into your header files, and making sure things which should be public slots are. David > Greetings > Stefan > >>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe << > >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<