View Full Version : 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!
aamer4yu
25th March 2010, 04:22
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 :rolleyes:
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 :rolleyes:
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.
aamer4yu
25th March 2010, 05:59
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 ;)
That's a good example. I'll read it and find out how it works.
Many thanks!
Problem solved.
I used my own way because the methods in Qt Demos will change too much of my code.
Thanks anyway.
Urthas
27th March 2010, 05:42
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.