Results 1 to 4 of 4

Thread: QGraphicsScene/QGraphicsView is selecting only one item

  1. #1
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsScene/QGraphicsView is selecting only one item

    Hello!

    Situation:
    I have a QGraphicsScene, QGraphicsView and several QGraphicsEllipseItems on it;
    I enabled selecting and moving for the ellipse items;

    What happens:
    When I double click on an item, it is being selected - that's ok.
    When I double click on another item then, it is not being selected, but the first still has its selection.

    I overrode the doubleMouseClick method of QGraphicsView and I made it clear all the items from slection, before calling its parent method. It looks like this
    Qt Code:
    1. void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent *event){
    2. QList<QGraphicsItem*> list = this->scene()->items();
    3. foreach(QGraphicsItem* item, list){
    4. item->setSelected(false);
    5. qDebug()<<"selection: "<<item->isSelected();
    6. }
    7. QGraphicsView::mouseDoubleClickEvent(event);
    8. foreach(QGraphicsItem* item, list){
    9. qDebug()<<"selection: "<<item->isSelected();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    It behaves now like this:
    before click: one item has isSelected()=true;
    after clicking, before calling parent method: all items have isSelected()=false;
    after calling parent method: the first item (not the one I clicked now) has isSelected()=true;

    So, as a result, the first clicked item "steals" the selection from the others.
    By the way, I checked the result of itemAt(mouse position) function, and it returns the proper item.

    I'm very confused, help would be really appreciated.
    Last edited by jjkrol; 28th May 2011 at 19:17.

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView is selecting only one item

    According to Qt's documentation the items() method returns the items in descending stacking order. Maybe the first item (topmost) is so large that it covers the other items also?
    I would rather show the selection visually in the scene than using qDebug(). It might be easier that way to see what really happens.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene/QGraphicsView is selecting only one item

    Quote Originally Posted by joyer83 View Post
    I would rather show the selection visually in the scene than using qDebug(). It might be easier that way to see what really happens.
    Modify your item's paint method to use the isSelected() setting to determine, for example, the outline color.

  4. #4
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene/QGraphicsView is selecting only one item

    The selection is also visible, and it stays on the first item I clicked. It doesn't also depend on the stack order - whichever item I click first, it stays selected all the time.

Similar Threads

  1. QGraphicsView, QGraphicsScene item coordinates in scene
    By harakiri in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2010, 10:02
  2. Selecting an item from QListWidget
    By chandru@080 in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2010, 06:30
  3. Replies: 0
    Last Post: 12th April 2010, 12:22
  4. selecting a specific QtreeList item
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 9th October 2007, 13:45
  5. QListWidget and selecting an item
    By invictus in forum Newbie
    Replies: 4
    Last Post: 19th June 2007, 11:59

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.