I checked the reference and found that the selectionChanged() signal is introduced since Qt4.3, and I am using Qt4.2 now![]()
I checked the reference and found that the selectionChanged() signal is introduced since Qt4.3, and I am using Qt4.2 now![]()
In that case you are better of to reimplement the item's itemChange method. Actually you can derive your item even from QObject and you can emit custom signal when selection changes.
Qt Code:
{ Q_OBJECT public: MyCustomItem() .. signals: void selectionChanged(bool newState); protected: { bool newState = value.toBool(); //This sometimes might not be safe to emit signal //here since the state is still unchanged. Better option might //be to use a single shot timer to emit this signal rather than //emitting it directly emit selectionChanged(newState); } } };To copy to clipboard, switch view to plain text mode
P.S: I'm not sure of my comment in the program though![]()
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
cwnelatury (12th May 2009), Shawn (30th July 2007)
Another way of catching events is -
Install filter on the scene, check if any item is presnt where the mouse is pressed. U can get the item by itemAt() function, and then perform the action u want.
However from the thumbnail I see u have more of a tree structure kind items.
I want to know why arent u using a tree itself ?? U can create a custom QTreeWidgetItem and have a pointer to the QGraphicsItem to it..
like -
class MyTreeWidgetItem : public QTreeWidgetItem
{
QGraphicsItem *pGraphicsItem;
};
and u can have a map of graphics items and tree nodes -
QHash <QGraphicsItem *, MyTreeWidgetItem*> item_treeMap;
Hope I am getting ur problem correctly .
Shawn (31st July 2007)
I guess this might not work. Actually even i stated above that eventFilter is an alternative.
Since the event filter is called before the event handlers the item wont be selected when mouse is pressed and control reaches eventFilter. So you have no way here to determine whether the item is selected or not. You cant even execute the event handler in eventfilter since the handlers are protected/private.
Please correct me if i am wrong
EDIT: Sorry misread your post. You select the item just by coordinate right(using itemAt). I thought you were looking at selectedItems in the event handler. But you have to make sure that you are checking the button and also set the state of the item to selected.
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
Shawn (31st July 2007)
I did what u said
Qt Code:
{ Q_OBJECT public: MyRectItem(); signals: void selectionChanged(bool newState); protected: { { bool newState = value.toBool(); emit selectionChanged(newState); } } };To copy to clipboard, switch view to plain text mode
Qt Code:
{ i->setZValue(2); }To copy to clipboard, switch view to plain text mode
and there is an error :
error C2440: 'initializing' : cannot convert from 'QGraphicsRectItem *' to 'MyRectItem *'
How can I deal with this ?
I am not familiar with the multi-inheritance.
Hope you can help me, and thanks in advance.
Last edited by Shawn; 31st July 2007 at 08:23.
The scene.addRect returns new QGraphicsRectItem* which is now way connected to MyRectItem other than that they both have common base.
To add any of your custom item use scene.addItem(new CustomItem()) (in your case new MyRectItem).
One more thing , you don't seem to store any rect coordinates nor provide the required constructor. One way to tackle this is to inherit MyRectItem from QGraphicsRectItem rather that QAbstractGraphicsShapeItem and implement the constructors as needed![]()
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
Shawn (31st July 2007)
I inherite MyRectItem from and implement the constructor
Qt Code:
{ Q_OBJECT public: MyRectItem(int x, int y, int w,int h) { } .... };To copy to clipboard, switch view to plain text mode
and then
But there is no Rect nowQt Code:
{ MyRectItem *i = new MyRectItem(x, y, m, n); scene.addItem(i); }To copy to clipboard, switch view to plain text mode![]()
I guess the following changes will fix your code
Firstly your constructor
and secondly set some pen and brush to the itemQt Code:
{}To copy to clipboard, switch view to plain text mode
Qt Code:
To copy to clipboard, switch view to plain text mode
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
Shawn (31st July 2007)
Yes I 've set pen and brush to the item, I discard them just to make my post short
And I revise the constructor as u said and it works !
Thanks a lot
the next problem is that :
seems no signal is emited ?
because when I select the MyRectItems one after another, and nothing happens?
Singal and Slot definition and connection is as below.
Latest:
Now it is OK ~Qt Code:
{ MyRectItem *i = new MyRectItem(x, y, m, n); scene.addItem(i); .... }To copy to clipboard, switch view to plain text mode
Last edited by Shawn; 31st July 2007 at 10:11.
You used the wrong signature of signal in the connect statement.
Shouldn't that be SIGNAL(selectionChanged(bool)) ?
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
Shawn (31st July 2007)
Yes, I also change the signal to make it more simple...
The problem is that the signal is emited whenever the selection of an item is changed, which also means that when I unselecte the item, the signal will also be emited.
This is not the what I want, which I want exactly is that when I selected an item, which would be better, double click an item, there can be a signal, and then I can write some slots to deal with this signal.
Thats very easy to fix
Notice that i changed the signal name to be more meaningful. Change everywhere as neededQt Code:
if(newState == true) emit itemSelected();To copy to clipboard, switch view to plain text mode![]()
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
Shawn (31st July 2007)
Bookmarks