Results 1 to 3 of 3

Thread: How to show/draw focus & select at the same time

  1. #1
    Join Date
    May 2006
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to show/draw focus & select at the same time

    Hi,

    I'm having difficulty selecting an item and drawing a focus rect on it at the same time.

    What I want to do is: in a single-selection mode tree widget if the last selected item does not pass a test when the user clicks a different item I want to move the selection & focus rect back to the last selected item. The last item can be re-selected without problem, however, the focus rect always seems to stay on the item the user just clicked.

    Please help if any of you has done this before.

    Qt Code:
    1. void MyForm::OnTreeWidgetItemSelectionChangedSlot()
    2. {
    3. . . .
    4.  
    5. //TreeWidget has single selection mode.
    6. //This slot is in response to the itemSelectionChanged signal.
    7.  
    8. SelectedItemList = TreeWidget->selectedItems();
    9.  
    10. if (SelectedItemList.count() > 0)
    11. {
    12. //Get the newly selected item.
    13. Item = SelectedItemList[0];
    14.  
    15. if (LastItem failed the validation test)
    16. {
    17. //Unselect the newly selected item.
    18. Item->setSelected(false);
    19.  
    20. //Set the selection & focus back to the last item.
    21. LastItem->setSelected(true);
    22. TreeWidget->setCurrentItem(LastItem);
    23. }
    24. else
    25. {
    26. LastItem = Item;
    27. ...
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    TM
    Last edited by wysota; 6th November 2006 at 17:48. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to show/draw focus & select at the same time

    QAbstractItemView behaves so that when a mouse press event is received, the selection is applied first and then the current index is set afterwards changing the selection. So by the time selection change is informed through signal, QAbstractItemView has not yet set the current index. So what happens here is that still if you change the current item to something else, QAbstractItemView comes and resets it back to the pressed index.

    One idea to solve this would be to delay the changing of the current item:
    Qt Code:
    1. // A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed
    2. QTimer::singleShot(0, this, SLOT(setLastItemCurrent()));
    3.  
    4. void MyForm::setLastItemCurrent()
    5. {
    6. // get "LastItem" somehow or store is as a member variable or something..
    7. TreeWidget->setCurrentItem(LastItem);
    8. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    tanminh (6th November 2006)

  4. #3
    Join Date
    May 2006
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to show/draw focus & select at the same time

    By the way, my interpretation of JPN's comments is no matter what we do in the itemSelectionChanged signal handler Qt will set the current item at the end of the mouse press event (that emitted the itemSelectionChanged signal in the first place). Using that idea I was able to do what I wanted with an additional itemClicked signal handler (whose itemClicked signal is fired at the end of a mouse press event, I think). Perhaps not a good way but it does do what I want. Thanks JPN. TM.

Similar Threads

  1. Tab/Enter focus problem
    By b1 in forum Qt Programming
    Replies: 4
    Last Post: 23rd October 2006, 23:34
  2. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48

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.