Results 1 to 3 of 3

Thread: Selecting items in a QTreeWidget with the keyboard

  1. #1
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Selecting items in a QTreeWidget with the keyboard

    In my QTreeWidget I have hooked up the itemClicked(QTreeWidgetItem*, int) to a slot onClicked(QTreeWidgetItem*, int) this works perfect for detecting selection with the mouse.

    I would like to allow keyboard navigation selection of my QTreeWidget, but I can't find a similar signal for when I select items via the keyboard (i.e. press space when the item has focus). Is there a signal I am overlooking that fires when items are selected via the keyboard and passes a pointer to the selected item?
    Last edited by Berryblue031; 18th May 2011 at 13:55.

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Selecting items in a QTreeWidget with the keyboard

    As an update I implemented the following solution, though I still find it odd that there was no built in mechanism (that I could find anyways)

    Qt Code:
    1. //I was already inheriting QTreeWidget
    2. //because this is a special built tree that I use in multiple places
    3. class ProductTreeWidget: public QTreeWidget
    4. {
    5. ...
    6. };
    7.  
    8. void ProductTreeWidget::keyPressEvent(QKeyEvent* event)
    9. {
    10. QTreeWidget::keyPressEvent(event);
    11. switch (event->key())
    12. {
    13. case Qt::Key_Space:
    14. case Qt::Key_Select:
    15. if(currentItem())
    16. {
    17. emit QTreeWidget::itemClicked(currentItem(), 0);
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    If anybody can see any obvious flaws to this approach pls post

  3. #3
    Join Date
    Jun 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selecting items in a QTreeWidget with the keyboard

    Well, this is on old thread, however not solved satisfactorily. You chose an overly complicated solution which I believe is not completely reliable. The easiest and most correct is to hook to QTreeView::selectionModel() signal called currentChanged(QModelIndex,QModelIndex). Example: connect(myTreeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(myTreeSelectionChanged(QModelIndex)));

    I am using QTreeView but QTreeWidget would be the same because it inherits from QTreeView. This solution works for me without any problems. Anyway you might also need to explore other signals of the selectionModel, they might be of some use for you.

Similar Threads

  1. removing items QTreeWidget
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2015, 22:58
  2. Replies: 1
    Last Post: 12th November 2009, 19:05
  3. The order of items in QTreeWidget
    By KK in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2009, 05:54
  4. amount of items in QTreeWidget
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2008, 23:38
  5. Replies: 1
    Last Post: 7th April 2006, 12:13

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.