I'm developing an application for use on both windows (I'm using windows 10, Qt 5.12.4 and mingw 7.3.0 32bit) and mac (High Sierra, Qt 5.12.4 and Clang 64 bit).
A part of the application consists of a QListWidget where I want to be able to move items up and down by dragging them.
In addition I'd like to do a couple of other things:
1) 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.

The problem is that while everything works fine on win, on macos when I drag'n'drop the items, the item being moved disappears at the drop.
I found that using the method setMovement(QListView::Snap) on the QListWidget fixes the drag'n'drop problem (items move fine also on mac), but completely breakes the rest of the behaviours: I cannot intercept the drag'n'drop events and when I try to access the items before closing the application I get a segfault.

Does anyone know what am I doing wrong or if is there a reasonable workaround?

Here's a small example to reproduce the issue:

dnd.pro
Qt Code:
  1. QT += core gui
  2.  
  3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  4.  
  5. CONFIG += c++11
  6.  
  7. # The following define makes your compiler emit warnings if you use
  8. # any Qt feature that has been marked deprecated (the exact warnings
  9. # depend on your compiler). Please consult the documentation of the
  10. # deprecated API in order to know how to port your code away from it.
  11. DEFINES += QT_DEPRECATED_WARNINGS
  12.  
  13. # You can also make your code fail to compile if it uses deprecated APIs.
  14. # In order to do so, uncomment the following line.
  15. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  16. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  17.  
  18. SOURCES += \
  19. main.cpp \
  20. mainwindow.cpp
  21.  
  22. HEADERS += \
  23. mainwindow.h
  24.  
  25. FORMS += \
  26. mainwindow.ui
  27.  
  28. # Default rules for deployment.
  29. qnx: target.path = /tmp/$${TARGET}/bin
  30. else: unix:!android: target.path = /opt/$${TARGET}/bin
  31. !isEmpty(target.path): INSTALLS += target
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include "mainwindow.h"
  2.  
  3. #include <QApplication>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. MainWindow w;
  9. w.show();
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode 

mainwindow.ui (default form for a Qt widgets application, did nothing in here)
Qt Code:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>800</width>
  10. <height>600</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralwidget"/>
  17. <widget class="QMenuBar" name="menubar"/>
  18. <widget class="QStatusBar" name="statusbar"/>
  19. </widget>
  20. <resources/>
  21. <connections/>
  22. </ui>
To copy to clipboard, switch view to plain text mode 

mainwindow.h
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QListWidget>
  6.  
  7. QT_BEGIN_NAMESPACE
  8. namespace Ui { class MainWindow; }
  9. QT_END_NAMESPACE
  10.  
  11. class MainWindow : public QMainWindow
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. MainWindow(QWidget *parent = nullptr);
  17. ~MainWindow();
  18.  
  19. private:
  20. Ui::MainWindow *ui;
  21. QVector <QListWidgetItem *> items;
  22. };
  23. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

mainwindow.cpp
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. this->layout()->addWidget(lw);
  19.  
  20. lw->setDragDropMode(QAbstractItemView::InternalMove);
  21. lw->setDefaultDropAction(Qt::MoveAction);
  22. 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 mac
  23.  
  24. items.append(new QListWidgetItem("item 1"));
  25. items.append(new QListWidgetItem("item 2"));
  26. items.append(new QListWidgetItem("item 3"));
  27. lw->addItem(items[0]);
  28. lw->addItem(items[1]);
  29. lw->addItem(items[2]);
  30.  
  31. // feature 1) intercept drag'n'drop events
  32. QAbstractItemModel * model = lw->model();
  33. connect(model, &QAbstractItemModel::rowsMoved, this, [=] (const QModelIndex &, int from, int, const QModelIndex &, int to)
  34. {
  35. qDebug() << "moved item from" << from << "to" << to;
  36. });
  37. }
  38.  
  39. MainWindow::~MainWindow()
  40. {
  41. // feature 2) operate on items before closing the application
  42. for (int i = 0; i < items.size(); i++)
  43. {
  44. qDebug() << items[i]->text();
  45. }
  46. delete ui;
  47. }
To copy to clipboard, switch view to plain text mode 

Thanks a lot for any input!