PDA

View Full Version : Why isSelected returns false when item is not movable?



ngugc
6th May 2012, 11:17
On Windows 7. Qt Creator 2.4.1 .
The project is compiled using MinGW.
The main code:

class DiagramItem : public QGraphicsObject
....
DiagramItem::DiagramItem()
{
//setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
//setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setCursor(Qt::OpenHandCursor);
}

void DiagramItem::contextMenuEvent(QGraphicsSceneContex tMenuEvent *event)
{
if( isSelected() ) {
QMessageBox::information(NULL, "Mosquito", "Selected.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}

QGraphicsItem::contextMenuEvent( event );
}

void DiagramItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
if (event->button() == Qt::LeftButton) {
if( contains( event->pos() ) ) {
scene()->clearSelection();
setSelected(true);
if( isSelected() ) {
QMessageBox::information(NULL, "Mosquito", "Selected.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}
//QMessageBox::information(NULL, "Mosquito", "Left Clicked.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}
}

QGraphicsItem::mouseReleaseEvent( event );
}

isSelected() in contextMenuEvent returns false without setFlag(QGraphicsItem::ItemIsMovable, true) in the ctor.
Why "selectable" property is related to "movable" property?
I don't want to make the item movable. How to make it seletable without movable?
The attachment is the code project.

amleto
6th May 2012, 12:14
according to the docs, selectable is not related to movable.


void QGraphicsItem::setSelected ( bool selected )

If selected is true and this item is selectable, this item is selected; otherwise, it is unselected.

If the item is in a group, the whole group's selected state is toggled by this function. If the group is selected, all items in the group are also selected, and if the group is not selected, no item in the group is selected.

Only visible, enabled, selectable items can be selected. If selected is true and this item is either invisible or disabled or unselectable, this function does nothing.

By default, items cannot be selected. To enable selection, set the ItemIsSelectable flag.

This function is provided for convenience, allowing individual toggling of the selected state of an item. However, a more common way of selecting items is to call QGraphicsScene::setSelectionArea(), which will call this function for all visible, enabled, and selectable items within a specified area on the scene.

See also isSelected() and QGraphicsScene::selectedItems().

ngugc
6th May 2012, 12:22
I didn't retype the code. It's the real complete code snippet.
And the attachment contains the full project.
Thanks!

amleto
6th May 2012, 13:00
after some debugging, it is the act of right-clicking that resets the selecteditems in the scene.


I didn't retype the code. It's the real complete code snippet.
And the attachment contains the full project.
Thanks!

you are responding to my sig - nice to see some people read it :lol:

ngugc
6th May 2012, 13:02
Note that, the isSelected() in mouseReleaseEvent() returns "true", while the one in contextMenuEvent() returns false.
I debugged it step by step:
I first triggered mouseReleaseEvent() by clicking one of the item with mouse left button, the messagebox was displayed, and the property QGraphicsItem::selected was set to "1"(true). Then triggered contextMenuEvent() by clicking the item with mouse right button, the property QGraphicsItem::selected was set to "0"(false) before contextMenuEvent() was invoked, so the messagebox wasn't shown.
After adding "setFlag(QGraphicsItem::ItemIsMovable, true)" in the constructor, everything works fine.

amleto
6th May 2012, 13:05
Note that, the isSelected() in mouseReleaseEvent() returns "true", ...

of course it does! you set it to true just before you call isSelected() :rolleyes:

ngugc
6th May 2012, 13:05
Oops! I didn't notice it's your sig. I'm new here, not familiar with the style.
Thanks! I debugged it and also found what you found.
But why does right-clicking reset it?

amleto
6th May 2012, 13:07
I have said what is turning the selection off. whether that is actually correct behaviour... dunno. submit a bug report to nokia :)



edit: hmm, actually, looking closer, this is what happens:


debug comment - is selected

*** LMB dowm***

before mouse press false
selectionchanged
after mouse press true

***LMB UP ***

before mouserelease true
after mouserelease true

***RMB DOWN***
before mouse press true
after mouse press true
selectionchanged <<<<<<<<<<<<<< this is peculiar imo.

*** RMB UP ***
NO mouse release event!

ngugc
6th May 2012, 13:09
Yes, It's used to confirm that setSelected() works.