Inconsistent QListWidget drag&drop behaviour on Win and macOS
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
Code:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp
Code:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
mainwindow.ui (default form for a Qt widgets application, did nothing in here)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QListWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
{
Q_OBJECT
public:
MainWindow
(QWidget *parent
= nullptr
);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBoxLayout>
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) , ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setCentralWidget(w);
w->setLayout(vl);
this->layout()->addWidget(lw);
lw->setDefaultDropAction(Qt::MoveAction);
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
lw->addItem(items[0]);
lw->addItem(items[1]);
lw->addItem(items[2]);
// 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;
});
}
MainWindow::~MainWindow()
{
// feature 2) operate on items before closing the application
for (int i = 0; i < items.size(); i++)
{
qDebug() << items[i]->text();
}
delete ui;
}
Thanks a lot for any input!
Re: Inconsistent QListWidget drag&drop behaviour on Win and macOS
I do not have a Mac, but your code also segfaults on exit with Linux 64-bit Qt 5.13.2.
When I run it from the command line i see this warning:
Code:
QMainWindowLayout
::addItem: Please use the
public QMainWindow API instead
In this code:
Code:
this->layout()->addWidget(lw);
you add the list widget to the layout of the main window not the layout of the central widget you just created. When I add it to the central widget the segfault issue disappears.
The items disappear when dropped outside the list widget, e.g. on the title bar of the main window. Items dragged within the list widget seem to behave as expected.
Re: Inconsistent QListWidget drag&drop behaviour on Win and macOS
Thanks ChrisW67 for replying!
Sorry for the wrong layout usage, but I put together the minimum code a bit in a hurry before leaving the office for some holidays.
Have you checked if the parts of code identified as features 1) and 2) work on your system?
Best
Re: Inconsistent QListWidget drag&drop behaviour on Win and macOS
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.
Re: Inconsistent QListWidget drag&drop behaviour on Win and macOS
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
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 mac
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 post
Quote:
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.
implemented with the following lines of code
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;
});
and
Code:
// feature 2) operate on items before closing the application
for (int i = 0; i < items.size(); i++)
{
qDebug() << items[i]->text();
}
cease to work.
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?
Re: Inconsistent QListWidget drag&drop behaviour on Win and macOS
Quote:
1) intercept these internal drag'n'drop events with a slot
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.
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()
Quote:
2) do operations on the items within the QListWidget in the window destructor before closing the application.
You create, and cache pointers to, three list items.
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:
Code:
for (int i = 0; i < lw->count(); ++i) {
qDebug() << i << lw->item(i)->text();
}
Re: Inconsistent QListWidget drag&drop behaviour on Win and macOS
Quote:
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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBoxLayout>
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) , 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;
}
and of course you need this declaration too in MainWindow's properties:
Thank you very much for your help!