Change order of rows in a QTableWidet by drag & drop
-
wrote on 14 Nov 2015, 21:03 last edited by
Hi my friends.
I want to ask if it possible to make a QTableWidget which permit to drag whole rows, for only change the order of rows.
It is necessary to subclass QTableWidget ? And how ? Have we some example in C++ ?
(I use Qt 5.5)greatings, Daniel
-
Hi,
IIRC, you just need to enabled Drag'n'Drop on your QTableWidget, set the selection mode to Row and the DradDropMode to InternalMove and you should be good to go.
Hope it helps
-
wrote on 14 Nov 2015, 22:18 last edited by
Thanks for your answer.
I tried
m_tagsTable->setDragEnabled(true);
m_tagsTable->setDragDropMode(QAbstractItemView::InternalMove);
m_tagsTable->verticalHeader()->setSectionsMovable(true);
but, if a have {1,2,3} or order of rows at beginning and drag row 1 to 3, then i have { nothing, 2,1} but not { 2,3,1}
Can you help me then ?
Daniel -
You're missing
m_tagsTable->setAcceptDrops(true);
-
wrote on 14 Nov 2015, 22:28 last edited by
Thanks, but it also not work :
m_tagsTable->setDragEnabled(true);
m_tagsTable->setDragDropMode(QAbstractItemView::InternalMove);
m_tagsTable->verticalHeader()->setSectionsMovable(true);
m_tagsTable->setAcceptDrops(true);
From {1,2,3} it become { nothing,2,1} (the origin row is deleted, without content)
Know you some other trick ? -
Sorry, my bad, using
m_tagsTable->verticalHeader()->setSectionsMovable(true);
should be enough unless you want to move the rows from selection. -
wrote on 14 Nov 2015, 22:44 last edited by
So, you know some way for reorder a QTableWidget ?
In a QListWidget, its easy to do, but with this instructions , which i show in last message, dont work for reorder.
This only move a row to a destination, but leave origin row in blanc.
Daniel -
Sorry, I wasn't clear in my last message. For QTableWidget and unless you want to move your rows based on selection (which would be unusual) you can remove the D'n'D stuff and only use setSectionsMovable on the vertical header.
-
wrote on 14 Nov 2015, 23:22 last edited by
I already understand this, but you not answer my answer.
May be i am not clear ?
I sent you a website in which already somebody ask this (for a previous Qt version) and receive a answer based on PyQt, but i dont understand well PyQt :
http://stackoverflow.com/questions/26227885/drag-and-drop-rows-within-qtablewidget/26311179#26311179
Use "setSectionsMovable" with or without D'n'D stuff, dont work.
Thanks for you pacience.
Daniel -
Before digging too deep into the D'n'D part
Does:int main(int argc, char *argv[]) { QApplication app(argc, argv); QTableWidget tw; tw.setRowCount(3); tw.setColumnCount(3); for (int i = 0 ; i < tw.rowCount() ; ++i) { for (int j = 0 ; j < tw.colorCount() ; ++j) { QTableWidgetItem *item = new QTableWidgetItem(QString("%1 %2").arg(i).arg(j)); tw.setItem(i, j, item); } } tw.verticalHeader()->setSectionsMovable(true); tw.show(); return app.exec(); }
do what you want ?
Note that when setting setSectionsMovable, you have to use the section "button" to move a row. -
wrote on 14 Nov 2015, 23:57 last edited by Daniel T
Thanks.
I already compiled this code i tryed it.
If i drag from (1,1) (which contains "0 0") and drag to (3,1), it simply select (1,1),(2,1) and (3,1)
I show you this result :
image
Sorry i dont now how to "incrust" a image here in this chat, so i give a link.
What i want to have as a result after dragging from "0 0" to "2 0" is :
"1 0" "1 1" "1 2"
"2 0" "2 1" "2 2"
"0 0" "0 1" "0 2"
So its like "insert".
Thanks any more help. Daniel -
Using setSectionsMovable, you don't select anything. Just put your mouse on the section number, press and move the section (just like you would do with e.g. Excel or Numbers)
-
wrote on 15 Nov 2015, 00:08 last edited by
Of course this is what do :
Make a click over "0,0". This will select "0,0". I drag my mouse until i am over "2 0", release, and i already show you which result i get.
No reorder, no insertion , only selection from "0 0" to "2 0"
Daniel -
wrote on 15 Nov 2015, 00:20 last edited by
Hi.
I found a way.
However dragging a cell not work, its work if a drag a vertical header.
Of course it would good if i can drag cells of column 0, but i am already satisfied for this moment.
Thanks. Daniel -
You're doing the opposite of what I ask you to try. By the way, if you want to move the columns you need to set the section movable property on the horizontal header (I know, this one is a bit confusing)
-
wrote on 15 Nov 2015, 00:51 last edited by
What i move are items of the header which is at left. I called it QVertcalHeader.
I am wrong ? Anyway, setting this QVertcalHeader to movable, permit me to move rows (moveing items on this header)
I also search for get a answer, which one is a QVerticalHeader, but for now this is for me more raisonable.
I only know to which say a "row header" at : https://documentation.devexpress.com/#WindowsForms/CustomDocument387
greatings, Daniel -
wrote on 15 Nov 2015, 17:13 last edited by
Hi Daniel, there was a bugQTBUG-48408 is it the same problem? Should be solved in Qt5.6.
-
@yeckel nop, that's not the same thing. That bug's about the selection model + sorting. Here the subject is moving a row up/down.
10/18