Results 1 to 7 of 7

Thread: Inconsistent QListWidget drag&drop behaviour on Win and macOS

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Dec 2017
    Location
    Cesena, Italy
    Posts
    13
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Inconsistent QListWidget drag&drop behaviour on Win and macOS

    What happens if you call only setDragDropMode() and not setDefaultDropAction() or setMovement()
    It seems this was enough to get everything working!
    That is, to get the behaviour I want I just need to call setDragDropMopde(QAbstractItemView::InternalMove). Any call to setDefaultDropAction() or setMovement() interferes somehow. I got confused cause on Win the call to setDefaultDropAction() does not change the overall behaviour (at least nothing I could notice), while on Mac it breaks the d'n'd, making items disappear whenever they are moved.
    I also implemented the further suggested fix (no items vector and calls to QListWidget::item).
    Here's for future reference the full mainwindow.cpp that works for me
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QBoxLayout>
    5. #include <QDebug>
    6.  
    7. MainWindow::MainWindow(QWidget *parent)
    8. : QMainWindow(parent)
    9. , ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12.  
    13. QWidget * w = new QWidget;
    14. this->setCentralWidget(w);
    15.  
    16. w->setLayout(vl);
    17.  
    18. lw = new QListWidget;
    19. vl->layout()->addWidget(lw);
    20.  
    21. lw->setDragDropMode(QAbstractItemView::InternalMove);
    22. // lw->setDefaultDropAction(Qt::MoveAction); REMOVED
    23. // lw->setMovement(QListView::Snap); REMOVED
    24.  
    25. lw->addItem(new QListWidgetItem("item 1"));
    26. lw->addItem(new QListWidgetItem("item 2"));
    27. lw->addItem(new QListWidgetItem("item 3"));
    28.  
    29. QAbstractItemModel * model = lw->model();
    30. connect(model, &QAbstractItemModel::rowsMoved, this, [=] (const QModelIndex &, int from, int, const QModelIndex &, int to)
    31. {
    32. qDebug() << "moved item from" << from << "to" << to;
    33. });
    34. }
    35.  
    36. MainWindow::~MainWindow()
    37. {
    38. for (int i = 0; i < lw->count(); i++)
    39. {
    40. qDebug() << lw->item(i)->text();
    41. }
    42. delete ui;
    43. }
    To copy to clipboard, switch view to plain text mode 
    and of course you need this declaration too in MainWindow's properties:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    Thank you very much for your help!

  2. The following user says thank you to fcona for this useful post:

    d_stranz (7th January 2020)

Similar Threads

  1. Replies: 0
    Last Post: 28th June 2019, 12:36
  2. Replies: 0
    Last Post: 13th December 2016, 12:22
  3. Replies: 2
    Last Post: 30th January 2014, 06:46
  4. Replies: 0
    Last Post: 8th December 2011, 12:21
  5. Replies: 3
    Last Post: 10th June 2010, 15:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.