Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-6058

QHeaderView gets confused when the underlying model gets reset() and sections have been moved

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Not Evaluated
    • 4.6.1
    • 4.5.3
    • Widgets: Itemviews
    • None
    • Debian stable, gcc.
    • b4dbbd30c8092a81984cca30248db087353fe66e

    Description

      I have a QAbstractTableModel class which actually acts as a proxy to an underlying data source. When the underlying data source changes I repopulate the QAbstractTableModel and call reset() on it. The number of columns in the model may change as a result.

      If, in a QTableView on top of this model, I have reordered the columns, the QHeaderView gets visually corrupted if the number of columns in the model has changed. Looking at the code, the problem lies in the visual<>logical mapping vectors d>logicalIndices and d->visualIndices in QHeaderView. QHeaderView::reset() explicitly doesn't call QHeaderViewPrivate::clear(), which would reset the logical<->visual mapping vectors. As a result, you can end up with visual sections which are mapped to non-existent logical sections.

      For example, suppose the logical sections are mapped

      {3, 1, 2}

      (i.e. the 3rd logical column is shown first, then the first, then the second). If the model is reset and now has only two columns, the mapping vectors simply get truncated and now hold

      {3,1}

      . As a result, the visual display is corrupted with missing columns and strange blank spaces in the table. There's no way to rectify this, as the only non-private access to the mapping vectors is through moveSection() or swapSection(), neither of which is capable of removing an out-of-bounds entry. At this stage the only solution is to create and set a completely new QHeaderView.

      To reproduce, run the attached example. Move the "Date" column in the "Sorted/Filtered model" section to be first (so the headers read Date, Subject, Sender). Press the "new Model" button.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            goffart Olivier Goffart (closed Nokia identity) (Inactive)
            mark_mackey Mark Mackey
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes