QListView in a QTabWidget tab - External Drop Not Working
-
wrote on 30 Jun 2019, 08:51 last edited by
Hi all,
I have now solved this.
As my drop was not item dependent, that is I didn't care what was under the drop, so long as it was the list, I didn't need to subclass the QStandardItemModel. Instead I simply subclassed the QListView with the following overridden methods:
void dragEnterEvent(QDragEnterEvent *e); void dropEvent(QDropEvent *ev); void dragMoveEvent(QDragMoveEvent* event);
I can now drop on my list and I will receive the filename of the file that was dropped. I then "emit" this filename to my parent to act on it accordingly.
Thanks for all your help.
Steve Q. :-)
-
Hi all,
I have now solved this.
As my drop was not item dependent, that is I didn't care what was under the drop, so long as it was the list, I didn't need to subclass the QStandardItemModel. Instead I simply subclassed the QListView with the following overridden methods:
void dragEnterEvent(QDragEnterEvent *e); void dropEvent(QDropEvent *ev); void dragMoveEvent(QDragMoveEvent* event);
I can now drop on my list and I will receive the filename of the file that was dropped. I then "emit" this filename to my parent to act on it accordingly.
Thanks for all your help.
Steve Q. :-)
wrote on 5 Nov 2021, 09:31 last edited byhi @steveq ,
I have encountered the same difficulties as you. then I follow your way :
setAceeptDrop(true)
and overridden methodsvoid dragEnterEvent(QDragEnterEvent *e); void dropEvent(QDropEvent *ev); void dragMoveEvent(QDragMoveEvent* event);
and add QDebug() in methods, but I don't see any output of console, and ListView still gives me the round circle with the line through it, indicating I can't drop. I'm obviously missing something here, but I just don't know what it is!
-
hi @steveq ,
I have encountered the same difficulties as you. then I follow your way :
setAceeptDrop(true)
and overridden methodsvoid dragEnterEvent(QDragEnterEvent *e); void dropEvent(QDropEvent *ev); void dragMoveEvent(QDragMoveEvent* event);
and add QDebug() in methods, but I don't see any output of console, and ListView still gives me the round circle with the line through it, indicating I can't drop. I'm obviously missing something here, but I just don't know what it is!
@Luzz-T Please show the code of the class where overriding these methods and show how you're using this class.
-
@Luzz-T Please show the code of the class where overriding these methods and show how you're using this class.
wrote on 5 Nov 2021, 09:43 last edited by@jsulm
I only want get file name by external drop. ListViewDrop class inherits QListViewListViewDrop.h
#ifndef LISTVIEWDROP_H #define LISTVIEWDROP_H #include <QListView> namespace Ui { class ListViewDrop; } class ListViewDrop : public QListView { Q_OBJECT public: explicit ListViewDrop(QWidget *parent = nullptr); ~ListViewDrop(); protected: void dragEnterEvent(QDragEnterEvent* ev) override; void dragMoveEvent(QDragMoveEvent *e) override; void dropEvent(QDropEvent* ev) override; }; #endif // LISTVIEWDROP_H
ListViewDrop.cpp
#include "listviewdrop.h" #include <qdebug.h> #include <QDragEnterEvent> #include <QDropEvent> #include <QMimeData> #include <QUrl> ListViewDrop::ListViewDrop(QWidget* parent): QListView(parent) { this->setAcceptDrops(true); } ListViewDrop::~ListViewDrop() { } void ListViewDrop::dragEnterEvent(QDragEnterEvent *ev) { qDebug() << "ListViewDrop::dragEnterEvent"; if(ev->mimeData()->hasUrls()) { ev->accept(); } //ev->ignore(); } void ListViewDrop::dropEvent(QDropEvent *ev) { qDebug() << "ListViewDrop::dropEvent"; if(ev->mimeData()->hasUrls()) { QList<QUrl> urls = ev->mimeData()->urls(); for(int i=0; i<urls.size(); i++) { qDebug() << urls.at(i).toLocalFile(); } } } void ListViewDrop::dragMoveEvent(QDragMoveEvent *e) { qDebug() << "ListViewDrop::dragMoveEvent"; }
I create a ListViewDrop is in a QWidget
Thanks heaps in advance.
-
@jsulm
I only want get file name by external drop. ListViewDrop class inherits QListViewListViewDrop.h
#ifndef LISTVIEWDROP_H #define LISTVIEWDROP_H #include <QListView> namespace Ui { class ListViewDrop; } class ListViewDrop : public QListView { Q_OBJECT public: explicit ListViewDrop(QWidget *parent = nullptr); ~ListViewDrop(); protected: void dragEnterEvent(QDragEnterEvent* ev) override; void dragMoveEvent(QDragMoveEvent *e) override; void dropEvent(QDropEvent* ev) override; }; #endif // LISTVIEWDROP_H
ListViewDrop.cpp
#include "listviewdrop.h" #include <qdebug.h> #include <QDragEnterEvent> #include <QDropEvent> #include <QMimeData> #include <QUrl> ListViewDrop::ListViewDrop(QWidget* parent): QListView(parent) { this->setAcceptDrops(true); } ListViewDrop::~ListViewDrop() { } void ListViewDrop::dragEnterEvent(QDragEnterEvent *ev) { qDebug() << "ListViewDrop::dragEnterEvent"; if(ev->mimeData()->hasUrls()) { ev->accept(); } //ev->ignore(); } void ListViewDrop::dropEvent(QDropEvent *ev) { qDebug() << "ListViewDrop::dropEvent"; if(ev->mimeData()->hasUrls()) { QList<QUrl> urls = ev->mimeData()->urls(); for(int i=0; i<urls.size(); i++) { qDebug() << urls.at(i).toLocalFile(); } } } void ListViewDrop::dragMoveEvent(QDragMoveEvent *e) { qDebug() << "ListViewDrop::dragMoveEvent"; }
I create a ListViewDrop is in a QWidget
Thanks heaps in advance.
@Luzz-T said in QListView in a QTabWidget tab - External Drop Not Working:
I create a ListViewDrop is in a QWidget
Please show how
-
@jsulm
I only want get file name by external drop. ListViewDrop class inherits QListViewListViewDrop.h
#ifndef LISTVIEWDROP_H #define LISTVIEWDROP_H #include <QListView> namespace Ui { class ListViewDrop; } class ListViewDrop : public QListView { Q_OBJECT public: explicit ListViewDrop(QWidget *parent = nullptr); ~ListViewDrop(); protected: void dragEnterEvent(QDragEnterEvent* ev) override; void dragMoveEvent(QDragMoveEvent *e) override; void dropEvent(QDropEvent* ev) override; }; #endif // LISTVIEWDROP_H
ListViewDrop.cpp
#include "listviewdrop.h" #include <qdebug.h> #include <QDragEnterEvent> #include <QDropEvent> #include <QMimeData> #include <QUrl> ListViewDrop::ListViewDrop(QWidget* parent): QListView(parent) { this->setAcceptDrops(true); } ListViewDrop::~ListViewDrop() { } void ListViewDrop::dragEnterEvent(QDragEnterEvent *ev) { qDebug() << "ListViewDrop::dragEnterEvent"; if(ev->mimeData()->hasUrls()) { ev->accept(); } //ev->ignore(); } void ListViewDrop::dropEvent(QDropEvent *ev) { qDebug() << "ListViewDrop::dropEvent"; if(ev->mimeData()->hasUrls()) { QList<QUrl> urls = ev->mimeData()->urls(); for(int i=0; i<urls.size(); i++) { qDebug() << urls.at(i).toLocalFile(); } } } void ListViewDrop::dragMoveEvent(QDragMoveEvent *e) { qDebug() << "ListViewDrop::dragMoveEvent"; }
I create a ListViewDrop is in a QWidget
Thanks heaps in advance.
wrote on 5 Nov 2021, 09:54 last edited by JonB 11 May 2021, 09:55@Luzz-T
First you must answer @jsulm's question.But I see two possible issues in your current code, which may (or may not) need addressing after you have sorted out why your drag methods are not being hit at all:
-
Your
dragMoveEvent(QDragMoveEvent *e)
does not accept the event. You need to have that as well asdragEnterEvent()
accept the drag, else you are liable to end up with the "no drop" indicator.dropEvent
should also accept it, I don't know if that matters in your case. -
In your
dragEnterEvent
you useev->accept();
. I'm not sure whether it matters, but here (and the other two methods) you are supposed to useacceptProposedAction()
.
-
-
@Luzz-T said in QListView in a QTabWidget tab - External Drop Not Working:
I create a ListViewDrop is in a QWidget
Please show how
wrote on 5 Nov 2021, 10:14 last edited by"create" actually I use qt Designer, Layout following :
In the picture above ,the red box is the “ListViewDrop” of my promotion by QListView,and "SelectView" used in main.cpp
#include "selectview.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); SelectView w; w.show(); return a.exec(); }
-
@Luzz-T
First you must answer @jsulm's question.But I see two possible issues in your current code, which may (or may not) need addressing after you have sorted out why your drag methods are not being hit at all:
-
Your
dragMoveEvent(QDragMoveEvent *e)
does not accept the event. You need to have that as well asdragEnterEvent()
accept the drag, else you are liable to end up with the "no drop" indicator.dropEvent
should also accept it, I don't know if that matters in your case. -
In your
dragEnterEvent
you useev->accept();
. I'm not sure whether it matters, but here (and the other two methods) you are supposed to useacceptProposedAction()
.
-
-
"create" actually I use qt Designer, Layout following :
In the picture above ,the red box is the “ListViewDrop” of my promotion by QListView,and "SelectView" used in main.cpp
#include "selectview.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); SelectView w; w.show(); return a.exec(); }
wrote on 5 Nov 2021, 10:36 last edited by JonB 11 May 2021, 10:49@Luzz-T
In addition to my earlier points, you may need to add the following:listView->setDropIndicatorShown(true);
Also if your list view has a model look at what
listView->model()->supportedDropActions()
is returning. P.S. And also probablyQAbstractItemModel::mimeTypes()
,dropMimeData()
,canDropMimeData()
too --- in a word, are you using a model with yourQListView
?I think you should implement your
dragMoveEvent()
now, as I found without that I got stick with a "no-entry" drop indicator all the time.The important is to confirm whether any of your drag/drop overrides report a
qDebug()
output as their first line?Finally, in Designer did you set any of the properties on your
listView
, there are several related to d&d? -
@Luzz-T
In addition to my earlier points, you may need to add the following:listView->setDropIndicatorShown(true);
Also if your list view has a model look at what
listView->model()->supportedDropActions()
is returning. P.S. And also probablyQAbstractItemModel::mimeTypes()
,dropMimeData()
,canDropMimeData()
too --- in a word, are you using a model with yourQListView
?I think you should implement your
dragMoveEvent()
now, as I found without that I got stick with a "no-entry" drop indicator all the time.The important is to confirm whether any of your drag/drop overrides report a
qDebug()
output as their first line?Finally, in Designer did you set any of the properties on your
listView
, there are several related to d&d?wrote on 5 Nov 2021, 16:50 last edited by Luzz T 11 May 2021, 16:51@JonB
Thank you for your suggestions. I found the problem. ThedragDropMode
attribute is not set, it defaults to
NoDragDrop
. But strangely, the default attribute does not work in Windows, and the default valueNoDragDrop
is OK in MacOS,
Also the version of QT may be different or for other reasonsFor all that It's already working. Thank all