Change order of rows in a QTableWidet by drag & drop
-
-
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
-
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);
-
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. -
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.
-
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. -
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)
-
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)
-
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