QTableView drag columns to move them



  • I'm using Qt 5.7 and am trying to move columns around by dragging them. Googling this I find that setSectionsMovable(true); should do the trick, but it doesn't.

    In my setup I have the following

        ui->tvRMA->setModel(&_displayModel);
        ui->tvRMA->setSortingEnabled(false);
        ui->tvRMA->setSelectionBehavior(QAbstractItemView::SelectRows);
        ui->tvRMA->horizontalHeader()->setSectionsMovable(true);
    

    This does everything that I want except allow me to drag columns around. What more needs to be done?

    Mike



  • Hi mjsurette,
    I think, because it is a view linked with a model, you have to implement some current model function with that be possible. You setting the view, you may have also to setting the model now.

    old thred on qtcentre.org:
    texte du lien
    texte du lien

    hope this will help you, tell us/share your solution when fixed.



  • Thank you @jerome_isAviable for your reply. I did check out your links, but sadly they were of no help.

    Changing the model in order to change the view breaks the whole model-view concept of separating the data from the display. Changing the model would change all views, not just the current one. Having movable columns should be on a per view basis for what I want.

    Your first link involves changing the model as you suggest, which is not what I need.

    The second link suggests calling setColumnMovingEnabled() which is the Qt3 equivalent of setSectionsMovable() if I'm reading this right.

    Any other thoughts?

    I will post the solution if/when I find it.

    Mike



  • Well, it now works. The problem is I don't know how. I started working on another feature and when I came back to this it was working. There is no call to setSectionsMovable(). I did a grep on 'mov' and only two results, neither of which have anything to do with this.

    For now, this is a mystery.



  • After much experimentation and some luck I found the solution to this mystery.

    On startup I had

    ui->tvRMA->horizontalHeader()->restoreState(...);
    

    which restored invalid information from previous sessions with different layouts. Clearing the database of previous sessions brought back the proper behaviour.

    Mike


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.