Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QStyledItemDelegate doesn't move with its QTreeView column
Forum Updated to NodeBB v4.3 + New Features

QStyledItemDelegate doesn't move with its QTreeView column

Scheduled Pinned Locked Moved Unsolved Qt for Python
pysidepythonqt for python
3 Posts 2 Posters 340 Views 2 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.
  • M Offline
    M Offline
    mjiggidyj
    wrote on last edited by mjiggidyj
    #1

    I find that item delegates, assigned to a column via QTreeView, are "sticking" to a particular visual column index, rather than following the logical column as it gets moved around in the model.

    In my QAbstractItemModel, I insert headers to the front of a list (basically self._headers.insert(0,headerdata) each time).

    In my QTreeView, I connect self.model().columnsInserted.connect(self._assignDelegates). In _assignDelegates(), I loop over the range of logical columns provided by the signal, and based on whatever the particular column is, I call self.setItemDelegateForColumn(col, delegate). (In my troubleshooting I've also tried converting the column index to visual via self.header().visualIndex(col), but this doesn't fix it.)

    So at this point, the first column of my QTreeView has the delegate assigned. But as soon as the next column is inserted, I would expect the existing delegate to shift down with its existing column. But instead, it seems that it "sticks" with visual column 0. So I'm essentially just always swapping out the delegate on visual column 0 over and over.

    But then also, if I drag around a column header to visually rearrange the column order in the QTreeView, the delegate follows correctly. So, geez, pick a behavior.

    What's the best way of solving this? Best I can think of is re-applying all delegates to every column every time any columns are inserted or moved in the model. But that's ridiculously inefficient IMO.

    jeremy_kJ 1 Reply Last reply
    0
    • M mjiggidyj

      I find that item delegates, assigned to a column via QTreeView, are "sticking" to a particular visual column index, rather than following the logical column as it gets moved around in the model.

      In my QAbstractItemModel, I insert headers to the front of a list (basically self._headers.insert(0,headerdata) each time).

      In my QTreeView, I connect self.model().columnsInserted.connect(self._assignDelegates). In _assignDelegates(), I loop over the range of logical columns provided by the signal, and based on whatever the particular column is, I call self.setItemDelegateForColumn(col, delegate). (In my troubleshooting I've also tried converting the column index to visual via self.header().visualIndex(col), but this doesn't fix it.)

      So at this point, the first column of my QTreeView has the delegate assigned. But as soon as the next column is inserted, I would expect the existing delegate to shift down with its existing column. But instead, it seems that it "sticks" with visual column 0. So I'm essentially just always swapping out the delegate on visual column 0 over and over.

      But then also, if I drag around a column header to visually rearrange the column order in the QTreeView, the delegate follows correctly. So, geez, pick a behavior.

      What's the best way of solving this? Best I can think of is re-applying all delegates to every column every time any columns are inserted or moved in the model. But that's ridiculously inefficient IMO.

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      @mjiggidyj said in QStyledItemDelegate doesn't move with its QTreeView column:

      In my QAbstractItemModel, I insert headers to the front of a list (basically self._headers.insert(0,headerdata) each time).

      In my QTreeView, I connect self.model().columnsInserted.connect(self._assignDelegates). In _assignDelegates(), I loop over the range of logical columns provided by the signal, and based on whatever the particular column is, I call self.setItemDelegateForColumn(col, delegate). (In my troubleshooting I've also tried converting the column index to visual via self.header().visualIndex(col), but this doesn't fix it.)

      This would be a lot easier to follow with a minimal working example, with emphasis on both minimal, and working to demonstrate the issue.

      So at this point, the first column of my QTreeView has the delegate assigned. But as soon as the next column is inserted, I would expect the existing delegate to shift down with its existing column. But instead, it seems that it "sticks" with visual column 0. So I'm essentially just always swapping out the delegate on visual column 0 over and over.

      But then also, if I drag around a column header to visually rearrange the column order in the QTreeView, the delegate follows correctly. So, geez, pick a behavior.

      I don't see an inconsistency. When a delegate for a column is assigned, the column number refers to the column returned by QModelIndex::column() at the time the delegate is drawn. Inserting a column before a particular item means that the index for that item will have a new column number. The model has been altered, and delegates are used by the view to render the model as it is, not as it was in the past.

      Dragging the columns in a view around will not result in a new column, because the underlying model hasn't changed.

      What's the best way of solving this? Best I can think of is re-applying all delegates to every column every time any columns are inserted or moved in the model. But that's ridiculously inefficient IMO.

      I don't understand what this is. My first thought is to suggest one or more of:

      • a proxy model to maintain the logical column concept
      • restructuring the model population
      • waiting for model population to complete before assignment of delegates, or even the view

      Perhaps code, images, or a reference to software implementing the desired interface will help.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      jeremy_kJ 1 Reply Last reply
      0
      • jeremy_kJ jeremy_k

        @mjiggidyj said in QStyledItemDelegate doesn't move with its QTreeView column:

        In my QAbstractItemModel, I insert headers to the front of a list (basically self._headers.insert(0,headerdata) each time).

        In my QTreeView, I connect self.model().columnsInserted.connect(self._assignDelegates). In _assignDelegates(), I loop over the range of logical columns provided by the signal, and based on whatever the particular column is, I call self.setItemDelegateForColumn(col, delegate). (In my troubleshooting I've also tried converting the column index to visual via self.header().visualIndex(col), but this doesn't fix it.)

        This would be a lot easier to follow with a minimal working example, with emphasis on both minimal, and working to demonstrate the issue.

        So at this point, the first column of my QTreeView has the delegate assigned. But as soon as the next column is inserted, I would expect the existing delegate to shift down with its existing column. But instead, it seems that it "sticks" with visual column 0. So I'm essentially just always swapping out the delegate on visual column 0 over and over.

        But then also, if I drag around a column header to visually rearrange the column order in the QTreeView, the delegate follows correctly. So, geez, pick a behavior.

        I don't see an inconsistency. When a delegate for a column is assigned, the column number refers to the column returned by QModelIndex::column() at the time the delegate is drawn. Inserting a column before a particular item means that the index for that item will have a new column number. The model has been altered, and delegates are used by the view to render the model as it is, not as it was in the past.

        Dragging the columns in a view around will not result in a new column, because the underlying model hasn't changed.

        What's the best way of solving this? Best I can think of is re-applying all delegates to every column every time any columns are inserted or moved in the model. But that's ridiculously inefficient IMO.

        I don't understand what this is. My first thought is to suggest one or more of:

        • a proxy model to maintain the logical column concept
        • restructuring the model population
        • waiting for model population to complete before assignment of delegates, or even the view

        Perhaps code, images, or a reference to software implementing the desired interface will help.

        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        Another option: Use a single custom delegate for the entire view, and look up the screen position of the index to determine the desired drawing style.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        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