1 Attachment(s)
Why isSelected returns false when item is not movable?
On Windows 7. Qt Creator 2.4.1 .
The project is compiled using MinGW.
The main code:
Code:
class DiagramItem : public QGraphicsObject
....
DiagramItem::DiagramItem()
{
//setFlag(QGraphicsItem::ItemIsMovable, true);
//setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setCursor(Qt::OpenHandCursor);
}
{
if( isSelected() ) {
}
}
{
if (event->button() == Qt::LeftButton) {
if( contains( event->pos() ) ) {
scene()->clearSelection();
setSelected(true);
if( isSelected() ) {
}
//QMessageBox::information(NULL, "Mosquito", "Left Clicked.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}
}
}
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.
Re: Why isSelected returns false when item is not movable?
according to the docs, selectable is not related to movable.
Quote:
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().
Re: Why isSelected returns false when item is not movable?
I didn't retype the code. It's the real complete code snippet.
And the attachment contains the full project.
Thanks!
Re: Why isSelected returns false when item is not movable?
after some debugging, it is the act of right-clicking that resets the selecteditems in the scene.
Quote:
Originally Posted by
ngugc
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:
Re: Why isSelected returns false when item is not movable?
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.
Re: Why isSelected returns false when item is not movable?
Quote:
Originally Posted by
ngugc
Note that, the isSelected() in mouseReleaseEvent() returns "true", ...
of course it does! you set it to true just before you call isSelected() :rolleyes:
Re: Why isSelected returns false when item is not movable?
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?
Re: Why isSelected returns false when item is not movable?
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:
Code:
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!
Re: Why isSelected returns false when item is not movable?
Yes, It's used to confirm that setSelected() works.