Results 1 to 7 of 7

Thread: how to get an ordered list of selected items in GraphicsScene

  1. #1
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Platforms
    Windows

    Question how to get an ordered list of selected items in GraphicsScene

    Hey, guys. I'm writing a program with pyqt4 and met a problem.

    In the class QGraphicsScene, there's a method "selectedItems()" which returns a list of all currently selected items. But the items are returned in no particular order. What if I want to get the first and second items that I selected? Is there any way to know the order when the items are selected?

    Thanks a lot!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get an ordered list of selected items in GraphicsScene

    I guess you will need to evaluate that yourself. One way is to monitor the QGraphicsItem::GraphicsItemChange , and keep a list of items that are selected.

    Also what do you want to do with first and second items selected ? If you can tell more, may be theres another solution

  3. #3
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: how to get an ordered list of selected items in GraphicsScene

    Quote Originally Posted by aamer4yu View Post
    I guess you will need to evaluate that yourself. One way is to monitor the QGraphicsItem::GraphicsItemChange , and keep a list of items that are selected.

    Also what do you want to do with first and second items selected ? If you can tell more, may be theres another solution
    This is what I'm thinking about! I wrote something and I know currently it's still not right. I want to connect two graphicsItem with an arrow, pointing from the first selected item to the second one. So I need the order.

    def itemChange(self, change, variant):
    if change == QGraphicsItem.ItemSelectedChange:
    ## I need some code here to give me a list "slctItems" of the currently selected items. I have a QGraphicsScene in a class called MainWindow which can get the selected items by calling selectedItems(). But this method (itemChange) is in another class. How could I call other class' method in one class? By the way, itemList will be used in another class too. So I made it global. Is it right?

    if len(slctItems) == 0:
    itemList = []
    if len(slctItems) == 1:
    itemList = slctItems
    if len(slctItems) == 2:
    if slctItems[0] == itemList[0]:
    itemList[1] = slctItems[1]
    else:
    itemList[1] = slctItems[0]
    if len(slctItems) > 2:
    itemList = []
    return QGraphicsItem.itemChange(self, change, variant)

    Really appreciate your help.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get an ordered list of selected items in GraphicsScene

    I want to connect two graphicsItem with an arrow, pointing from the first selected item to the second one. So I need the order.
    I bet you haven't looked at Qt Demos completely. Look in the Diagram Scene example under Qt Demos-->>GraphicsView

  5. #5
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: how to get an ordered list of selected items in GraphicsScene

    That's a good example. I'll read it and find out how it works.

    Many thanks!

  6. #6
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: how to get an ordered list of selected items in GraphicsScene

    Problem solved.

    I used my own way because the methods in Qt Demos will change too much of my code.

    Thanks anyway.

  7. #7
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: how to get an ordered list of selected items in GraphicsScene

    Would it be an option to override QGraphicsScene::mousePressEvent to get the selected item and store the pointer in a list? If the length of the list == 2, draw a line from first to second item. From there it depends on what behavior you want: you could clear the list every time a line is drawn or copy the second element into the first and just keep refreshing the second element with the newly selected item, drawing line to line to line.

Similar Threads

  1. transfer graphicsscene on form1 to graphicsscene on form2
    By rogerholmes in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2009, 20:37
  2. Problem with item selected in a list
    By 0xl33t in forum Qt Programming
    Replies: 2
    Last Post: 3rd August 2009, 17:07
  3. ItemIsUserCheckable and checking all selected items
    By edice in forum Qt Programming
    Replies: 5
    Last Post: 28th May 2009, 13:14
  4. Getting indexes of selected items
    By gyre in forum Newbie
    Replies: 2
    Last Post: 20th November 2007, 19:23
  5. Get list of selected rows from QTableView
    By jnk5y in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 16:59

Tags for this Thread

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.