Results 1 to 5 of 5

Thread: QTreeView horizontal tab key navigation

  1. #1
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QTreeView horizontal tab key navigation

    Hi,

    when the user presses TAB while editing an item in a QTreeView, the view closes the current editor and starts editing the item below. Is there a way to change this behavior so that the item on the right is edited? (similar to LibreOffice Calc and probably Excel)
    I tried setting event filters and overwriting the event-function in the tree, the viewport and the editor (using a custom delegate), but I was not able to intercept this TAB event.

    Below you will find a simple test script (in PyQt), without event filters.

    Thanks

    Qt Code:
    1. from PyQt5 import QtCore, QtWidgets
    2. from PyQt5.QtCore import Qt
    3. #from PyQt4 import QtCore, QtGui as QtWidgets
    4. #from PyQt4.QtCore import Qt
    5.  
    6. class TreeView(QtWidgets.QTreeWidget):
    7. def __init__(self):
    8. super().__init__()
    9. self.setColumnCount(5)
    10. for i in range(3):
    11. item = QtWidgets.QTreeWidgetItem(["Some", "Test", "Strings"])
    12. item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsEnabled)
    13. self.addTopLevelItem(item)
    14.  
    15.  
    16. app = QtWidgets.QApplication([])
    17. tree = TreeView()
    18. tree.show()
    19. app.exec_()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView horizontal tab key navigation

    If it should behave more like a spreadsheet, maybe use QTableView?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTreeView horizontal tab key navigation

    The test script looks like a table, but in the real application I need a tree structure.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTreeView horizontal tab key navigation

    I think the correct approach here is to provide override QTreeView::moveCursor(), do something different for MoveNext and MovePrevious, and fall back to the base behaviour for the other options.

  5. The following user says thank you to ChrisW67 for this useful post:

    maranos (30th April 2014)

  6. #5
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTreeView horizontal tab key navigation

    Thank you, this solves the problem!

    Here is a simple implementation (not covering the cases where the next/previous index has a different parent):
    Qt Code:
    1. def moveCursor(self, cursorAction, modifiers):
    2. if cursorAction == QtWidgets.QAbstractItemView.MoveNext:
    3. index = self.currentIndex()
    4. if index.isValid():
    5. if index.column()+1 < self.model().columnCount():
    6. return index.sibling(index.row(), index.column()+1)
    7. elif index.row()+1 < self.model().rowCount(index.parent()):
    8. return index.sibling(index.row()+1, 0)
    9. else:
    10. return QtCore.QModelIndex()
    11. elif cursorAction == QtWidgets.QAbstractItemView.MovePrevious:
    12. index = self.currentIndex()
    13. if index.isValid():
    14. if index.column() >= 1:
    15. return index.sibling(index.row(), index.column()-1)
    16. elif index.row() >= 1:
    17. return index.sibling(index.row()-1, self.model().columnCount()-1)
    18. else:
    19. return QtCore.QModelIndex()
    20. super().moveCursor(cursorAction, modifiers)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 22nd August 2013, 10:21
  2. QTreeView Horizontal Scrollbar issue
    By Dehumanizer in forum Qt Programming
    Replies: 6
    Last Post: 18th July 2011, 10:41
  3. page navigation
    By ningoji in forum Newbie
    Replies: 2
    Last Post: 5th April 2011, 11:15
  4. Horizontal scroll bar on QTreeView
    By LynneV in forum Qt Programming
    Replies: 7
    Last Post: 9th November 2010, 21:05
  5. Replies: 0
    Last Post: 5th July 2010, 09:05

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
  •  
Qt is a trademark of The Qt Company.