The corrections I made in #2 to your code from #1 make the crash-on-exit disappear and reordering work correctly for me.
Dragging an item off the list widget removes it completely either way.
The corrections I made in #2 to your code from #1 make the crash-on-exit disappear and reordering work correctly for me.
Dragging an item off the list widget removes it completely either way.
I understand that the correction you suggested corrects the crash on exit, but that's not my problem (it was an error I introduced in the minimum project example and does not affect my full project). The problem is that if I comment this line
the drag'n'drop works only on win, while if I uncomment it the drag'n'drop works also on mac, but the two other features described in the original postQt Code:
lw->setMovement(QListView::Snap); // when this line is commented drag'n'drop does not work on mac, when it is uncommented features 1) and 2) do not work on both win and macTo copy to clipboard, switch view to plain text mode
implemented with the following lines of code1) intercept these internal drag'n'drop events with a slot
2) do operations on the items within the QListWidget in the window destructor before closing the application.
andQt Code:
// feature 1) intercept drag'n'drop events connect(model, &QAbstractItemModel::rowsMoved, this, [=] (const QModelIndex &, int from, int, const QModelIndex &, int to) { qDebug() << "moved item from" << from << "to" << to; });To copy to clipboard, switch view to plain text mode
cease to work.Qt Code:
// feature 2) operate on items before closing the application for (int i = 0; i < items.size(); i++) { qDebug() << items[i]->text(); }To copy to clipboard, switch view to plain text mode
Is there anything I can do to get everything working, i.e. reading "moved item from # to #" when an item is moved and reading the list of items' text when I close the application?
Calling setMovement() with Free or Snap interferes with signals on Linux also (Static is the default). I see no signals but visually and internally the DND works.1) intercept these internal drag'n'drop events with a slot
I assume you are making this call to try to get DND working on Mac (you say it is not).
What happens if you call only setDragDropMode() and not setDefaultDropAction() or setMovement()
You create, and cache pointers to, three list items.2) do operations on the items within the QListWidget in the window destructor before closing the application.
When these are added to the list widget the widget takes ownership.
When the setMovement() is called with Free or Snap I notice that drag and drop actions cause the widget (and internal model) to delete and create new items. This invalidates the cached pointers to the item(s) concerned. When you access the items through your cached pointers you risk a seg fault. I suggest:
- Dispense with the items vector
- Keep a pointer to the list widget
- Create and add the items directly to the list widget
- Access the items in the widget using the widget's API:
Qt Code:
for (int i = 0; i < lw->count(); ++i) { qDebug() << i << lw->item(i)->text(); }To copy to clipboard, switch view to plain text mode
fcona (7th January 2020)
It seems this was enough to get everything working!What happens if you call only setDragDropMode() and not setDefaultDropAction() or setMovement()
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
and of course you need this declaration too in MainWindow's properties:Qt Code:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QBoxLayout> #include <QDebug> , ui(new Ui::MainWindow) { ui->setupUi(this); this->setCentralWidget(w); w->setLayout(vl); vl->layout()->addWidget(lw); // lw->setDefaultDropAction(Qt::MoveAction); REMOVED // lw->setMovement(QListView::Snap); REMOVED connect(model, &QAbstractItemModel::rowsMoved, this, [=] (const QModelIndex &, int from, int, const QModelIndex &, int to) { qDebug() << "moved item from" << from << "to" << to; }); } MainWindow::~MainWindow() { for (int i = 0; i < lw->count(); i++) { qDebug() << lw->item(i)->text(); } delete ui; }To copy to clipboard, switch view to plain text mode
Thank you very much for your help!
d_stranz (7th January 2020)
Bookmarks