Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTreeview keyboard behaviour
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTreeview keyboard behaviour

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sirius79
    wrote on last edited by
    #1

    I'm displaying file hierarchies in a QTreeView and usually navigate it with the keyboard. When there are lots of files an a directory, it's very hard to collapse it - you have to scroll up until you hit the parent and hit the "left" key.

    I'm used to the behaviour that hitting left on a "leaf" also collapses its parent. Is there a way to set this behaviour with QTreeView?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      override the keyPressEvent handler of your tree view:
      @
      void MyTreeView::keyPressEvent(QKeyEvent* event)
      {
      QModelIndex currentIndex = this->currentIndex();
      if( event->key() == Qt::Key_Left && currentIndex.isValid() )
      this->collapse( this->isExpanded( currentIndex ) ? currentIndex : currentIndex.parent() );
      else
      QTreeView::keyPressEvent(event);
      }
      @
      (Not tested though)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sirius79
        wrote on last edited by
        #3

        Thanks a lot - a good and straight forward solution. Why didn't I think of this..?

        There is one issue with the solution - when you hit left now on a "leaf", the current selection vanishes and you're beamed to the root element as soon as you hit another key. The whole behaviour is strange, when you enter the QTreeView with the "Tab" key instead of clicking with the mouse, you need to push "left" twice on leaves to collapse them etc.

        I found that the canonical solution - updating the current element manually to its parent - solves these issues.

        Problem solved - thanks so much!

        Working solution:
        @
        void SirTreeView::keyPressEvent(QKeyEvent* event)
        {
        QModelIndex currentIndex = this->currentIndex();
        if( event->key() == Qt::Key_Left && currentIndex.isValid() )
        {
        if (this->isExpanded( currentIndex ))
        {
        this->collapse(currentIndex);
        } else {
        this->collapse(currentIndex.parent());
        this->setCurrentIndex(currentIndex.parent());
        }
        }
        else
        QTreeView::keyPressEvent(event);
        }
        @

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved