Important: Please read the Qt Code of Conduct -

QHeaderView drag & drop, with a Model

  • I think I'm missing something fundamental about dragging and dropping columns. My basic setup is

    QAbstractItemModel -> QSortFilterProxyModel -> QTableView -> QHeaderView.
    All are subclassed

    void my_header::set_defaults()
    /Enable dragging & dropping of columns within the view/
    setDragDropMode( QAbstractItemView::InternalMove );
    setMovable( true );
    setDragEnabled( true );
    setDropIndicatorShown( true );

    Dragging & Dropping works fine. I can drag headers around, and the sectionMoved( int, int, int ) signal is emitted. My QTableView picks this up so it always knows what index is in what order. As such, my QSortFilterProxy model also always knows the correct order. I save these properly to QSettings, so future instances are started in the same order.

    However, things go pear-shaped when I want to hide a column, and filterAcceptsColumn() returns false. I'm doing something to map each column to the correct column in the parent model. However, it seems something fishy happens to the indexes once once of them is not accepted. A simple example would be a 3-column table view. When the application first starts, the columns would be displayed as indexes 0, 1, and 2. If the user drags and drops 0 from the far left to the far right, they'd be displayed as 1, 2, and 0. headerData(), and data() work fine at this point. However, if I want to hide column 0 via filterAcceptsColumn() returning false, then the view will reference the columns as indexes 0, and 1. I then have to do all types of dancing around to figure out which index is actually the refused one. Not ideal when there are some columns hidden, and some out of order, in no predictable pattern.

    That got a little wordy, and I think I'm missing a fundamental component of having my my view & headerview (hopefully automatically) tell the proxy that the indexes have moved. For what it's worth, I have connected QSortFilterProxyModel::columnsAboutToBeMoved() to a slot to debug, and it is seemingly never called. I've read the Drag & Dropping with Model/View doc, but it seems to mainly cover Mime encoding.

    Any help is appreciated.

Log in to reply