[Qt-qml] How to interface between main.cpp classes and custom c++ based widgets?

Gregory Schlomoff gregory.schlomoff at gmail.com
Mon Jul 11 21:29:44 CEST 2011


Hi,

First of all, I think your context property should start with a lower case
letter, otherwise, this will almost certainly not work.

So:
view.rootContext()->setContextProperty("locationProvider",
&locationProvider);

Next, let's assume your LocationProvider class has a Q_PROPERTY called
gpsCoordinates. Then, in your qml file, you could do something along the
lines of:

 Map {
  id: map
  position: locationProvider.gpsCoordinates
 }

And that's all. LocationProvider call should emit the gpsCoordinatesChanged
signal, and QML will listen and react to it.


On Mon, Jul 11, 2011 at 12:28 PM, Till Harbaum / Lists <lists at harbaum.org>wrote:

> Hi,
>
> i am porting the UI of my cacheme application to qml. Most things somehow
> work, some even nicely (making map overlays
> is a snap) and some are rather tricky (a nice file selector). But there is
> one thing i can't figure out how to do correctly:
>
> I have a widget which is a ggraphicsgeomap. This widget of course needs to
> interface to various things like the
> gps. My original plain qt had a global instance of my locationProvider
> class and i connected this to a slot in my mapwidget instance.
>
> But how do i do that with qml in between? My main.cpp contains an instance
> of my locationProvider, so i need a connect
> my custom widget with the instance only known to my main function. I think
> one step is to register my locationProvider with the
> declarative view:
>
>  LocationProvider locationProvider;
>  view.rootContext()->setContextProperty("LocationProvider",
> &locationProvider);
>
> in my qml part i create an instance of my map class  (my map is registered
> in c++ via
> qmlRegisterType<MapWidget>("MapWidget", 1, 0, "Map"), so the widget is
> created this way:
>
>  Map {
>   id: map
>  }
>
> Now the question: How do i connect a signal from my locationProvider with a
> slot of my map?
>
> Regards,
>  Till
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
>



-- 
Gregory Schlomoff
http://www.gregschlom.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20110711/1b9a946c/attachment.html 


More information about the Qt-qml mailing list