PDA

View Full Version : descending z order



dreamer
6th May 2008, 23:33
I'd like to listing all my items in the scene whose bounding rectangle is fully contained inside the scene rect area, and i'd like to have them listed in descending z order.....
From the reference document i found:
1)


QList<QGraphicsItem *> QGraphicsScene::items ( const QPointF & pos ) const;

Returns all visible items at position pos in the scene. The items are listed in descending Z order

2)


QList<QGraphicsItem *> QGraphicsScene::items ( const QRectF & rectangle, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

Returns all visible items that, depending on mode, are either inside or intersect with the specified rectangle;

How can i obtain my behaviour???

marcel
7th May 2008, 18:18
Use the second approach by passing the scene rect and sort the returned list by QGraphicsItem::zValue.

dreamer
7th May 2008, 18:23
is there any class dealing with sorting? Or i have to implement my sorting alghoritm, like quicksort or mergesort??

spud
7th May 2008, 18:31
Use void qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan ) (http://doc.trolltech.com/4.3/qtalgorithms.html#qSort-2). You will have to define your own lessThan function.

dreamer
7th May 2008, 18:44
i have proble defining my own less than function.........

" no matching function for call to ‘qSort(QList<QGraphicsItem*>::iterator, QList<QGraphicsItem*>::iterator, <unresolved overloaded function type>)"

header:


bool zOrderlessThan(const QGraphicsItem*,const QGraphicsItem*);

source:


bool myclas::zOrderlessThan(const QGraphicsItem* i1,const QGraphicsItem* i2){
return i1->zValue() > i2->zValue();
}

????????

jpn
12th May 2008, 14:43
Either make it a static function or don't make it a member function at all.

dreamer
12th May 2008, 15:27
making the function static, i resolve the problem of the function visibility, but i don't resolve an other problem dealing with the iterator.

I have a List: QList<QGraphicsItem*> objects and i'd like to use qsort on this list.
So i define my static function:


static bool zOrderlessThan(QList<QGraphicsItem*>::const_iterator i1,QList<QGraphicsItem*>::const_iterator i2){
return (*i1)->zValue() > (*i2)->zValue();}


and then i call it, from my source.cpp:

qsort(objects.begin(),objects.end(),zOrderlessThan );

and i obtain this error:


error: cannot convert ‘QList<QGraphicsItem*>::iterator’ to ‘void*’ for argument ‘1’ to ‘void qsort(void*, size_t, size_t, int (*)(const void*, const void*))’


What's wrong???????????

jpn
12th May 2008, 16:51
// main.cpp
#include <QtGui>

bool zValueGreaterThan(QGraphicsItem* item1, QGraphicsItem* item2)
{
return item1->zValue() > item2->zValue();
}

int main()
{
// some dummy items with z-values [0..3]
QList<QGraphicsItem*> items;
for (int i = 0; i < 3; ++i)
{
QGraphicsItem* item = new QGraphicsLineItem;
item->setZValue(i);
items += item;
}

qDebug() << "ascending order:\n" << items;

qSort(items.begin(), items.end(), zValueGreaterThan);

qDebug() << "descending order:\n" << items;
}

dreamer
12th May 2008, 17:09
ok..........thx very much. following your code , the problem goes away.:D

Bitto
14th May 2008, 22:42
Note, sorting by Z order is not the same as sorting by stacking order. Usually stacking order is what you want :-).

http://doc.trolltech.com/4.3/qgraphicsitem.html#setZValue

At the very least you should add that if the Z values are the same (which is very common), the pointers are compared to ensure sort stability. And Z value sorting only applies to immediate siblings, so if you want the items sorted by stacking order, your algorithm needs to compare Z values for immediate siblings of the two items' closest common ancestor :-).

Btw dreamer, the items() function that takes a QRectF also returns the items in descending Z order by default...