Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Moving a row to another position



  • Hello,
    I have a simple QListView with some text data(QStringListModel) as rows. See attached picture. Can anyone give me an hint how I can move Item3 up to the first position by clicking und pressing the mouse button? I see in the QListView signals pressed and clicked. Don't I need also a signal released.
    This should the end result:
    Item3
    Item1
    Item2

    The source code is here. It is just a dummy application.

    Bildschirmfoto von 2019-11-23 08-16-45.png


  • Lifetime Qt Champion

    You can move rows in a QStringListModel with moveRows().
    But what exactly do you want to achieve? Moving the rows by a button click or per drag'n'drop? The latter is already possible without extra work on your side.



  • Hi,
    thanks. I want to move the rows per drag and drop. No buttons.


  • Lifetime Qt Champion

    @ppp1 said in Moving a row to another position:

    I want to move the rows per drag and drop.

    Then set dragDropMode of the view to DragDrop and it works.



  • Thank. "Drag and drop" was the key. I have activated this mode with

    ui->listView->dragDropMode()
    

    But it doesn't work. Maybe I have to remove the pressed event of the list view. I will try.



  •         ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
            ui->listView->setDragEnabled(true);
            ui->listView->viewport()->setAcceptDrops(true);
            ui->listView->setDropIndicatorShown(true);
            ui->listView->setDragDropMode(QAbstractItemView::InternalMove);
    

    Now it deletes the item at the destination index. I will try later.



  •         ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
            ui->listView->setDragEnabled(true);
            ui->listView->viewport()->setAcceptDrops(true);
            ui->listView->setDropIndicatorShown(true);
            ui->listView->setDragDropMode(QAbstractItemView::InternalMove);
            ui->listView->setDragDropOverwriteMode(false);
    

    The selected data overwrites the existing item when dropped. The last command does not work. Any hints?


  • Lifetime Qt Champion

    @ppp1 said in Moving a row to another position:

    Any hints?

    It does work for me with a small testcase in the qt designer with QListWidget. What's not needed is the viewport()->setAcceptDrops() call. What Qt version and Os do you use?



  • Windows 10 Pro. QT 5.13.1
    Is it possible to upload you testcase? Or you can test my project? When my testcase works on your system, than I know that something is wrong with my system. Here is my project: Link


  • Lifetime Qt Champion

    Simply create a new widget ui in designer, add a QTreeWidget, add some items, set the flags and do a preview.



  • Found the answer: https://www.walletfox.com/course/qtreorderablelist.php. Variant 2.
    Note: The explanation for why we need to override flags() can be found in the documentation of the method bool dragDropOverwriteMode() const. The documentation says that the default behaviour when moving items is items' removal. To prevent the removal of the items it is necessary to reimplement flags() in such way that we do not return Qt::ItemIsDropEnabled.


Log in to reply