Nominate our 2022 Qt Champions!

Change order of rows in a QTableWidet by drag & drop

  • 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

  • Lifetime Qt Champion


    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
    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 ?

  • Lifetime Qt Champion

    You're missing m_tagsTable->setAcceptDrops(true);

  • Thanks, but it also not work :
    From {1,2,3} it become { nothing,2,1} (the origin row is deleted, without content)
    Know you some other trick ?

  • Lifetime Qt Champion

    Sorry, my bad, using m_tagsTable->verticalHeader()->setSectionsMovable(true); should be enough unless you want to move the rows from selection.

  • 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.

  • Lifetime Qt Champion

    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 :
    Use "setSectionsMovable" with or without D'n'D stuff, dont work.
    Thanks for you pacience.

  • Lifetime Qt Champion

    Before digging too deep into the D'n'D part

    int main(int argc, char *argv[]) 
        QApplication app(argc, argv);
        QTableWidget tw;
        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);
        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 :
    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

  • Lifetime Qt Champion

    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)

  • 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"

  • 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

  • Lifetime Qt Champion

    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 :
    greatings, Daniel

  • Hi Daniel, there was a bugQTBUG-48408 is it the same problem? Should be solved in Qt5.6.

  • Lifetime Qt Champion

    @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.

Log in to reply