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. Swapping columns on QTreeView causes row highlight to disconnect
Forum Updated to NodeBB v4.3 + New Features

Swapping columns on QTreeView causes row highlight to disconnect

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 123 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.
  • P Offline
    P Offline
    peter.thompson
    wrote on last edited by peter.thompson
    #1

    I'm facing a visual issue with QTreeView and am wondering if anyone knows a fix for it. I'm swapping the first column ("Name") and second column ("#") in my QTreeView for reasons explained below. The code I'm using to do this is:

    myTreeView->header()->swapSections(0, 1);
    

    I've also tried calling moveSection() on the header instead. Either way, the columns do get moved as expected. However, the highlighting on the rows is then visually disconnected between the two columns:

    a7d84034-e308-4eb5-9df7-f68c56e54f84-image.png

    Without swapping, the highlighting is connected, as desired:

    73528b9e-98b3-4eda-bef5-221557ecf0f4-image.png

    Is there a way I can make it so that the highlighting is connected even when the columns are swapped?


    The reason I'm swapping these columns is because I'm reusing a model for several different collections of data. Some collections are flat, as shown above, and some collections are hierarchical, as shown below:

    aa34ef55-d256-4492-9b1a-6b8c6e56c98f-image.png

    In the case of a flat collection, I do care about the data in the "#" column, and I want to display that column in the left-most position to help the user think about it as an index. But in the case of a hierarchical collection, I want to hide that data from the user, since indexing in a hierarchical context isn't quite so simple. But, as far as I am aware, in order to be displayed correctly by a QTreeView, a QAbstractItemModel needs its indexes to always be parented on the left-most column, and it needs that column to not be hidden. So the "#" column can't be the parent column, and it can't be the left-most column (at least not logically). So instead, I'm making the "Name" column be the parent column and putting it in the left-most position (logically). I thought I could just rearrange the columns to make the "#" be left-most (visually) in the case of a flat collection, but that has the issue described above.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Moving the first row around in a tree view is generally not a good idea because it is a special row with decorations. There are some bugs around wrt to this.
      Use a QSortFilterProxyModel or similar to change the contents or do it directly in the source model.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Moving the first row around in a tree view is generally not a good idea because it is a special row with decorations. There are some bugs around wrt to this.
        Use a QSortFilterProxyModel or similar to change the contents or do it directly in the source model.

        P Offline
        P Offline
        peter.thompson
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks for your response. I was worried that might be the case. Changing the model or implementing a proxy model will be a bigger change to the code, but should be doable.

        1 Reply Last reply
        0
        • P peter.thompson has marked this topic as solved on

        • Login

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