Results 1 to 9 of 9

Thread: Why isSelected returns false when item is not movable?

  1. #1
    Join Date
    May 2012
    Posts
    5

    Default 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:
    Qt Code:
    1. class DiagramItem : public QGraphicsObject
    2. ....
    3. DiagramItem::DiagramItem()
    4. {
    5. //setFlag(QGraphicsItem::ItemIsMovable, true);
    6. setFlag(QGraphicsItem::ItemIsSelectable, true);
    7. //setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    8. setCursor(Qt::OpenHandCursor);
    9. }
    10.  
    11. void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    12. {
    13. if( isSelected() ) {
    14. QMessageBox::information(NULL, "Mosquito", "Selected.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    15. }
    16.  
    17. QGraphicsItem::contextMenuEvent( event );
    18. }
    19.  
    20. void DiagramItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
    21. {
    22. if (event->button() == Qt::LeftButton) {
    23. if( contains( event->pos() ) ) {
    24. scene()->clearSelection();
    25. setSelected(true);
    26. if( isSelected() ) {
    27. QMessageBox::information(NULL, "Mosquito", "Selected.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    28. }
    29. //QMessageBox::information(NULL, "Mosquito", "Left Clicked.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    30. }
    31. }
    32.  
    33. QGraphicsItem::mouseReleaseEvent( event );
    34. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why isSelected returns false when item is not movable?

    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().
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    May 2012
    Posts
    5

    Default 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!

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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 View Post
    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:
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    May 2012
    Posts
    5

    Default 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.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why isSelected returns false when item is not movable?

    Quote Originally Posted by ngugc View Post
    Note that, the isSelected() in mouseReleaseEvent() returns "true", ...
    of course it does! you set it to true just before you call isSelected()
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    May 2012
    Posts
    5

    Default 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?

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. debug comment - is selected
    2.  
    3. *** LMB dowm***
    4.  
    5. before mouse press false
    6. selectionchanged
    7. after mouse press true
    8.  
    9. ***LMB UP ***
    10.  
    11. before mouserelease true
    12. after mouserelease true
    13.  
    14. ***RMB DOWN***
    15. before mouse press true
    16. after mouse press true
    17. selectionchanged <<<<<<<<<<<<<< this is peculiar imo.
    18.  
    19. *** RMB UP ***
    20. NO mouse release event!
    To copy to clipboard, switch view to plain text mode 
    Last edited by amleto; 6th May 2012 at 12:18.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #9
    Join Date
    May 2012
    Posts
    5

    Default Re: Why isSelected returns false when item is not movable?

    Yes, It's used to confirm that setSelected() works.

Similar Threads

  1. QSqlQueryModel insertColumn() returns false
    By kasper360 in forum Newbie
    Replies: 2
    Last Post: 28th March 2011, 04:58
  2. Qimage::save returns false in release
    By CCTeam in forum Qt Programming
    Replies: 5
    Last Post: 26th May 2010, 17:35
  3. QSslSocke::supportSsl() returns false
    By oscar in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2008, 18:51
  4. QSqlQuery::isValid returns false
    By mismael85 in forum Qt Programming
    Replies: 24
    Last Post: 7th September 2008, 23:43
  5. connect returns false
    By krivenok in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 20:01

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.