[Qt-qml] QDeclarativeItem subclasses and mouse events

Jan Ekholm jan.ekholm at smultron.net
Tue Aug 3 10:36:42 CEST 2010


On Tuesday 03 August 2010 09:51:13 michael.brasser at nokia.com wrote:
> Hi,
> 
> On 03/08/2010, at 4:27 PM, ext Jan Ekholm wrote:
> > On Tuesday 03 August 2010 04:55:46 Gregory Schlomoff wrote:
> >> Hello !
> >> 
> >> You may want to have a look at our code for drag and drop, it's working
> >> well:
> >> 
> >> http://bitbucket.org/gregschlom/qml-drag-drop
> >> 
> >> (and more specifically
> >> http://bitbucket.org/gregschlom/qml-drag-drop/src/tip/DeclarativeDragAre
> >> a.c pp )
> > 
> > You seem to do nothing magical with your item either. Strange. But you
> > also
> > 
> > have no implementation of:
> >    virtual QPainterPath shape () const;
> >    virtual QRectF boundingRect () const;
> 
> Note that QDeclarativeItem has a default implementation of boundingRect()
> based on the width/height of the item.

I found my problem. Or, well, not found, but cured. I needed a total clean 
recompile for it to work. Then my shape() started working as it should and the 
event handlers were called as expected. Apparently the fact that my class 
changed base class from QObject to QDeclarativeItem was a change too big for 
qmake to pick up and a clean compilation was needed.

However, I noticed that as I move the item to be a pure C++ item I also want 
to create it from the C++ side and later add to a Flickable in a QML file. 
Like this:

ectangle {
    id: playfield
 
    Flickable {
        contentWidth: hexes.width
        contentHeight: hexes.height
        anchors.fill: parent
 
        // the C++ class which contains 100-10000 C++ items of a Hex class
        Map {
            id: hexes
        }
    }
}

If I create a Map class on the C++ side, can I add it to the "scene" 
dynamically? I understand that a declarative thingie is not meant to be 
dynamic in any way, but I have to be a bit pragmatic. If the Map is created as 
above I can't refer to it from the C++ side, so, what I guess I want to do is 
something like:

Map * map = new Map;
map->setupContents();
map->doOtherSetup();
map->create10000Hexes();

// now find the flickable and make it show my map
QDeclarativeView * v = ....
Flickable * f = v->....
f->setItem ( map );

// profit

My rubbish little test app can be seen at:

	http://gitorious.org/tactics

But most stuff there is totally crap so don't go copying any ideas or code, 
it's very likely to be buggy, wrong and useless.

-- 
Voodoo is a very interesting religion for the whole family, even those
    members of it who are dead.
                          -- Terry Pratchett & Neil Gaiman, Good Omens
		       



More information about the Qt-qml mailing list