Results 1 to 10 of 10

Thread: descending z order

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question descending z order

    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)
    Qt Code:
    1. QList<QGraphicsItem *> QGraphicsScene::items ( const QPointF & pos ) const;
    To copy to clipboard, switch view to plain text mode 
    Returns all visible items at position pos in the scene. The items are listed in descending Z order

    2)
    Qt Code:
    1. QList<QGraphicsItem *> QGraphicsScene::items ( const QRectF & rectangle, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
    To copy to clipboard, switch view to plain text mode 
    Returns all visible items that, depending on mode, are either inside or intersect with the specified rectangle;

    How can i obtain my behaviour???

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: descending z order

    Use the second approach by passing the scene rect and sort the returned list by QGraphicsItem::zValue.

  3. #3
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: descending z order

    is there any class dealing with sorting? Or i have to implement my sorting alghoritm, like quicksort or mergesort??

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: descending z order

    Use void qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan ) . You will have to define your own lessThan function.

  5. #5
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: descending z order

    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:
    Qt Code:
    1. bool zOrderlessThan(const QGraphicsItem*,const QGraphicsItem*);
    To copy to clipboard, switch view to plain text mode 
    source:
    Qt Code:
    1. bool myclas::zOrderlessThan(const QGraphicsItem* i1,const QGraphicsItem* i2){
    2. return i1->zValue() > i2->zValue();
    3. }
    To copy to clipboard, switch view to plain text mode 
    ????????
    Last edited by dreamer; 7th May 2008 at 17:59. Reason: updated contents

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: descending z order

    Either make it a static function or don't make it a member function at all.
    J-P Nurmi

  7. #7
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: descending z order

    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:
    Qt Code:
    1. static bool zOrderlessThan(QList<QGraphicsItem*>::const_iterator i1,QList<QGraphicsItem*>::const_iterator i2){
    2. return (*i1)->zValue() > (*i2)->zValue();}
    To copy to clipboard, switch view to plain text mode 

    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???????????

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: descending z order

    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. bool zValueGreaterThan(QGraphicsItem* item1, QGraphicsItem* item2)
    5. {
    6. return item1->zValue() > item2->zValue();
    7. }
    8.  
    9. int main()
    10. {
    11. // some dummy items with z-values [0..3]
    12. QList<QGraphicsItem*> items;
    13. for (int i = 0; i < 3; ++i)
    14. {
    15. item->setZValue(i);
    16. items += item;
    17. }
    18.  
    19. qDebug() << "ascending order:\n" << items;
    20.  
    21. qSort(items.begin(), items.end(), zValueGreaterThan);
    22.  
    23. qDebug() << "descending order:\n" << items;
    24. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: descending z order

    ok..........thx very much. following your code , the problem goes away.
    Last edited by dreamer; 12th May 2008 at 16:15.

  10. #10
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: descending z order

    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/qgraphi...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...
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

Similar Threads

  1. problem with order of libs during linking
    By minimax in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 10:32
  2. Saving/Restoring columns order of QTreeWidget
    By mchara in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 08:43
  3. Tab Order Settings in a QDialog widgets
    By vinnu in forum Qt Programming
    Replies: 10
    Last Post: 21st September 2006, 16:25
  4. Replies: 1
    Last Post: 26th February 2006, 05:52
  5. Changing the order of columns in QTreeView
    By johnny_sparx in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 00:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.