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, disable deselection ability
Forum Updated to NodeBB v4.3 + New Features

[Solved] QTreeView, disable deselection ability

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.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.
  • Y Offline
    Y Offline
    YuriQ
    wrote on last edited by
    #1

    Qt version = 5.2.1

    Hello.
    I have QTreeView instance with few rows of data which cannot be deleted because "read-only" mode is turned on. When program starts it automatically selects 1st row using QItemSelectionModel::setCurrentIndex. Now I need to disable ability to select "nothing" by clicking on empty area of table or by another method. In other words my QTreeView instance must always have one of the items as current item. In other words it's selectionModel()->currentIndex() must always return valid index.
    I hope this is clear description of my goal. So how can I do it?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DBoosalis
      wrote on last edited by
      #2

      I think you can create your own class from QTreeView and implement its mousePressEvent(QMouseEvent *mouseEvent).

      Inside your mousePressEvent method you can get the coordinate of the mousepress event and use QTreeView::indexAt(const QPoint & point).

      If index is invalid simply do not allow mouseEvent to propagated by setting: mouseEvent->accept(); And if it is on a valid index simply pass the event through with QListView::mousePressEvent(mouseEvent)

      The downside here is that mouse clicks on scrollbar areas might be caught as well, but I am sure you can get around it though

      1 Reply Last reply
      0
      • B Offline
        B Offline
        butterface
        wrote on last edited by
        #3

        You just need to override selectionChanged() and skip it, if the new selection is empty.

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          YuriQ
          wrote on last edited by
          #4

          Thanks for answers. Overrideing selectionChanged is very easy to implement and it works for me.

          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