Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListView in a QTabWidget tab - External Drop Not Working

QListView in a QTabWidget tab - External Drop Not Working

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steveq
    wrote on 30 Jun 2019, 08:51 last edited by
    #10

    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. :-)

    L 1 Reply Last reply 5 Nov 2021, 09:31
    0
    • S steveq
      30 Jun 2019, 08:51

      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. :-)

      L Offline
      L Offline
      Luzz T
      wrote on 5 Nov 2021, 09:31 last edited by
      #11

      hi @steveq ,

      I have encountered the same difficulties as you. then I follow your way : setAceeptDrop(true) and overridden methods

      	void 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!

      J 1 Reply Last reply 5 Nov 2021, 09:33
      0
      • L Luzz T
        5 Nov 2021, 09:31

        hi @steveq ,

        I have encountered the same difficulties as you. then I follow your way : setAceeptDrop(true) and overridden methods

        	void 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!

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 5 Nov 2021, 09:33 last edited by
        #12

        @Luzz-T Please show the code of the class where overriding these methods and show how you're using this class.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 1 Reply Last reply 5 Nov 2021, 09:43
        1
        • J jsulm
          5 Nov 2021, 09:33

          @Luzz-T Please show the code of the class where overriding these methods and show how you're using this class.

          L Offline
          L Offline
          Luzz T
          wrote on 5 Nov 2021, 09:43 last edited by
          #13

          @jsulm
          I only want get file name by external drop. ListViewDrop class inherits QListView

          ListViewDrop.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.

          J JonBJ 2 Replies Last reply 5 Nov 2021, 09:44
          0
          • L Luzz T
            5 Nov 2021, 09:43

            @jsulm
            I only want get file name by external drop. ListViewDrop class inherits QListView

            ListViewDrop.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.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 5 Nov 2021, 09:44 last edited by
            #14

            @Luzz-T said in QListView in a QTabWidget tab - External Drop Not Working:

            I create a ListViewDrop is in a QWidget

            Please show how

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            L 1 Reply Last reply 5 Nov 2021, 10:14
            1
            • L Luzz T
              5 Nov 2021, 09:43

              @jsulm
              I only want get file name by external drop. ListViewDrop class inherits QListView

              ListViewDrop.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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 5 Nov 2021, 09:54 last edited by JonB 11 May 2021, 09:55
              #15

              @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 as dragEnterEvent() 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 use ev->accept();. I'm not sure whether it matters, but here (and the other two methods) you are supposed to use acceptProposedAction().

              L 1 Reply Last reply 5 Nov 2021, 10:31
              0
              • J jsulm
                5 Nov 2021, 09:44

                @Luzz-T said in QListView in a QTabWidget tab - External Drop Not Working:

                I create a ListViewDrop is in a QWidget

                Please show how

                L Offline
                L Offline
                Luzz T
                wrote on 5 Nov 2021, 10:14 last edited by
                #16

                @jsulm

                "create" actually I use qt Designer, Layout following :
                d4a95f13-7ed3-4d57-a8c2-f599b91dfd95-image.png

                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();
                }
                
                
                JonBJ 1 Reply Last reply 5 Nov 2021, 10:36
                0
                • JonBJ JonB
                  5 Nov 2021, 09:54

                  @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 as dragEnterEvent() 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 use ev->accept();. I'm not sure whether it matters, but here (and the other two methods) you are supposed to use acceptProposedAction().

                  L Offline
                  L Offline
                  Luzz T
                  wrote on 5 Nov 2021, 10:31 last edited by
                  #17

                  @JonB

                  Thanks for the tip. This is a test code, which I will pay attention to in development

                  1 Reply Last reply
                  0
                  • L Luzz T
                    5 Nov 2021, 10:14

                    @jsulm

                    "create" actually I use qt Designer, Layout following :
                    d4a95f13-7ed3-4d57-a8c2-f599b91dfd95-image.png

                    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();
                    }
                    
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 5 Nov 2021, 10:36 last edited by JonB 11 May 2021, 10:49
                    #18

                    @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 probably QAbstractItemModel::mimeTypes(), dropMimeData(), canDropMimeData() too --- in a word, are you using a model with your QListView?

                    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?

                    L 1 Reply Last reply 5 Nov 2021, 16:50
                    0
                    • JonBJ JonB
                      5 Nov 2021, 10:36

                      @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 probably QAbstractItemModel::mimeTypes(), dropMimeData(), canDropMimeData() too --- in a word, are you using a model with your QListView?

                      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?

                      L Offline
                      L Offline
                      Luzz T
                      wrote on 5 Nov 2021, 16:50 last edited by Luzz T 11 May 2021, 16:51
                      #19

                      @JonB
                      Thank you for your suggestions. I found the problem. The dragDropMode attribute is not set, it defaults to
                      NoDragDrop . But strangely, the default attribute does not work in Windows, and the default value NoDragDrop is OK in MacOS,
                      Also the version of QT may be different or for other reasons

                      For all that It's already working. Thank all

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved