Important: Please read the Qt Code of Conduct -

QTableView Fixing Some Columns

  • Hi all,

    I have two tableview which have shared custom model. In one of the tables, I want that some of the columns should be used for drag/drop, but in the other table user should be free to drag drop everything.

    For example first 3 columns will be fixed. and other columns should be movable inside themselves. they shoudnt be able to moved to first 3 columns section.

    How can I do that. It is important for me to implement.

    Thanks in advance

  • @DzCode
    One possibility would be to introduce a QAbstractProxyModel on top of your model as the source for one of the table views. Start from QIdentityProxyModel, and just change the drag/drops allowed. Now attach one view to the original model and the other to the proxy model.

  • @JonB They have to use the same custom model, they have some shared properties and each time one table is updated, allso other one updated.

    But my original question is also about one table. I want to make some columns movable, some not in the view and not affaectin the other table

  • @DzCode
    I don't understand.

    I thought you had one table, two views.

    If you have separate table models, one of which is to allow more dragging than the other, then make the models return different values for what can be dragged.

  • @JonB I have two different TableView with the same model

  • @DzCode
    Then I do not understand your:

    and each time one table is updated, allso other one updated.

    since there is only one model to update.

  • @JonB

    The thing I want is below:

    Think that there is only one table
    The first 3 columns are fixed in the table. (A,B,C)

    Other column headers are: D,E,G.

    D,E,G column headers should be movable between themselves. However, they should not be dragged and droped to the A,B,C columns region in the table.
    I only want to move D,E,G between column order 4 and 6. not dragged to first 3 columns' place.

  • Lifetime Qt Champion


    You will need a custom header view or a filter that ignores the mouse move event on these columns and possible the mouse press as well.

  • @SGaist Hi SGaist,

    How can I handle in mousemoveevent. inside my tableview, I am trying to get the modelindex from the mouse position and checking for column. But it does not work.

  • @SGaist

    I implemented a new QHeaderView and in mousereleaseevent:

    void CustomHeaderView::mouseReleaseEvent(QMouseEvent *event)
        if (event->pos().x() > sectionViewportPosition(2) && event->pos().x() < sectionViewportPosition(3))

    This actually solves my problem with a bug. This ignores the event when I want to release mouse over the 2nd column header.
    Problem 1:
    2020-09-14 13_06_39-TestApp.png
    Problem 2:
    This ruins my sortable columns. When I want to sort the table according to 2nd column, because fo the return from mouserelease I cannot sort the table.

  • Lifetime Qt Champion

    Did you try to just ignore the mouseMoveEvent on these columns rather than release ?

  • @SGaist thanks. it solves the problem

  • Lifetime Qt Champion

    Great !

    Since you have it working now, please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)

Log in to reply