Moving QTableView Moving Lines

I am using QTableView with QAbstractTableModel and QSortFilterProxyModel to sort items by clicking on table headers. I would like to add for the user the ability to sort the rows in the view manually by dragging them. I don't need to be able to drag and drop from / to any external application, just to reorder the list. I also do not need to change the data in the model, I just want the order to be different in the view.

I was looking through the documentation and it seems to me that I should implement mimeTypes , mimeData and dropMimeData , but it is very fast! Some data in my model does not actually appear in the view, and, as I said, I do not want to change the order of the data in the model. Is there a way to just drag and drop elements to change their sorting (just like the headers can already do) without a huge amount of encodings?

+6
source share
1 answer

Updated for QT5 to remove obsolete methods.

If you are using PyQT, all you need to do for your requirements is:

 your_tableview.verticalHeader().setSectionsMovable(True) your_tableview.verticalHeader().setDragEnabled(True) your_tableview.verticalHeader().setDragDropMode(QAbstractItemView.InternalMove) 

Then rinse and repeat with horizontalHeader top horizontalHeader if you want them to rearrange too.

You are absolutely right, you do not need to touch or even know which model for this functionality . This is also demonstrated by your correct use of the QSortFilterProxyModel decorator on the model itself.

The material that you saw about mimeTypes and all this is designed to drag and drop real objects from various sources from other windows / applications / desktop / etc. And not needed for what you are trying to accomplish at the moment.

+10
source

Source: https://habr.com/ru/post/924014/


All Articles