Unsolved Minimal example to drag-reorder rows in QTableView?
-
I am having a hard time figuring out how to reorder rows in my QTableView with a custom model. It uses SelectRows selection behavior:
self.setSelectionBehavior(QAbstractItemView.SelectRows)
Does anyone have or know of a minimal example to get this to work? I am starting to get a little frustrated with repeated failures.
Thanks!
-
I'm afraid the problem is that none of Qt's models implement moving rows so you'll have to code the behaviour manually
-
@patrickkidd
Although as @VRonin comments Qt's models do not implement moving rows, and that would mean you'd have to roll your own code completely, since you say you are using a custom model you could/might be able to implement:-
Include a column for "row number" in your model, if you can do that (if you can't, no dice). Note that this is what would be required if your model were a database and you needed to impose a row order for some purpose.
-
When a row is dragged in the view to reorder, you must implement the updating of that row's row number, plus all other rows after it which will will also need re-numbering.
-
You can now use, for example, a
QSortFilterProxy
between model & view which sorts by the model's row number column to implement reordering.
Whether this is easy/appropriate for your model/view only you will know.
-
-
Isn't it simpler to just make a reorder proxy model and put it over the original model, and then put the sort-filter proxy on top of it?
It's kind of the reasons proxies exist in the first place. -
@kshegunov
It might be, if we (I) knew what a "a reorder proxy model" was, or you provided a link to it.... :) -
That'd be your proxy model derived from the identity proxy.
-
@kshegunov
And how/where would this proxy model I'm going to write store the information on what the desired order is, please? -
@JonB said in Minimal example to drag-reorder rows in QTableView?:
And how/where would this proxy model I'm going to write store the information on what the desired order is, please?
Inside the proxy model itself; you provide mapping between the source and destination model indices similarly to how the sort-filter proxy does it.
-
@kshegunov
Ah, I see, not an area I have looked at.Presumably that would work provided OP does not want to permanently store the ordered items in a database (which may be the case). Without a permanent extra column for the order there will be nothing to restore the order on subsequent data re-read.
-
@JonB This is for implementing a list of layers as in photoshop, and stored in the document. So yes, an 'order' column will be necessary in the model.
It does help me to know that dragging and reordering entire rows is not supported by the QTableView/model code. So after my reading of the docs and qt source code, I am still a little lost at where to start writing this feature. For example, how deeply I need to re-write this functionality; which qt api parts to use and which to reimplement? Can you give a starting point?
-
Hi,
It's model specific and its normal that Qt doesn't provide default implementation for them. That ensure that the model doesn't do anything behind the back. The AbstractItemModel class can't presume what type of structure it is going to be put on top of.