Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Drop files to the mainWindow (QDeclarativeView)
Qt 6.11 is out! See what's new in the release blog

Drop files to the mainWindow (QDeclarativeView)

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 2 Posters 8.6k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    giesbert
    wrote on last edited by
    #2

    QDeclarativeView is a QScrollArea. So I think you also have to set the setAcceptDrops on the viewport. and have an event filter on the view port for the drag/drop events.

    Nokia Certified Qt Specialist.
    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hoozi
      wrote on last edited by
      #3

      [quote author="Gerolf" date="1311710917"]QDeclarativeView is a QScrollArea. So I think you also have to set the setAcceptDrops on the viewport. and have an event filter on the view port for the drag/drop events.

      [/quote]

      Thanks Gerolf.

      i add code

      in main.cpp
      @viewer.viewport()->setAcceptDrops(true);@

      in myviewer.cpp
      @void QAbstractScrollArea::dragEnterEvent(QDragEnterEvent *event)
      {
      event->accept();
      }@

      but still not work.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #4

        You did not add an eventFilter on the viewport right?
        have a look at QObject::installEventFilter

        additionally, what I normally do is setting setAcceptDrops on the main window and implements the drag/drop events also there.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hoozi
          wrote on last edited by
          #5

          can you send me your example code , please

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #6

            No I can't, my code is closed source for my boss, sorry.

            But you can have a look at this wiki page: "Drag and Drop of files":http://developer.qt.nokia.com/wiki/Drag_and_Drop_of_files

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hoozi
              wrote on last edited by
              #7

              Really thanks, Gerolf.

              but the key of my question is QDeclarativeView

              I use QML , so my Window is Inherited from QDeclarativeView , below is the Inherrited tree :

              MyView <---------- I work here
                 ┗QmlApplicationViewer <-----------Qt Creator wizard generated
                       ┗QDeclarativeView <----------- must inherited form this class, because use QML
                             ┗QGraphicsView
                                   ┗QAbstractScrollArea
                                         ┗QFrame
                                               ┗QWidget <-------- if I inherited from QWidget direct , drop OK!
                                                     ┗QObject

              Why?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #8

                That's not 100% correct

                @
                MyView <————— I work here
                ┗QmlApplicationViewer <—————-Qt Creator wizard generated
                ┗QDeclarativeView <—————- must inherited form this class, because use QML
                ┗QGraphicsView
                ┗QAbstractScrollArea
                ┗QFrame
                ┗QWidget <———— if I inherited from QWidget direct , drop OK!
                ┗QObject
                ┗> content QWidget as viewport <-- here the drop events will come to!!
                @

                if MyView is your main window then set drops on viewport and myView and use an event filter on the view port of the scroll area, which will receive the drop events.

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hoozi
                  wrote on last edited by
                  #9

                  I tried installEventFilter , my code here
                  @
                  MyView::MyView(QWidget *parent)
                  : QmlApplicationViewer(parent)
                  {

                  setAcceptDrops(true);
                  
                  this->viewport()->setAcceptDrops(true);
                  
                  this->viewport()->installEventFilter(this);
                  

                  }

                  bool MyView::eventFilter(QObject *obj, QEvent *event)
                  {
                  if (obj == this->viewport() ) {
                  if (event->type() == QEvent::DragEnter) {
                  QDragEnterEvent enterEvent = static_cast<QDragEnterEvent>(event);
                  qDebug() << "enter" ;
                  enterEvent->accept();

                          return true;
                      } else {
                          return false;
                      }
                  } else {
                      return QmlApplicationViewer::eventFilter(obj, event);
                  }
                  

                  }
                  @

                  I understand this Method can intercept all the event . Thus Catch the DragEnter event.

                  but my Problem is not catch event . first posted code Already can catch DragEnter event, but MyView not accept drag, Mouse display a forbidden cursor .

                  when used installEventFilter , the problem Still......................

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #10

                    [quote author="hoozi" date="1311771920"]but my Problem is not catch event . first posted code Already can catch DragEnter event, but MyView not accept drag, Mouse display a forbidden cursor.
                    [/quote]

                    Ah, no we come closer to the problem.
                    You have to accept the drag/drop operation in dragEnter:

                    @
                    void DocumentWindow::dragEnterEvent(QDragEnterEvent* event)
                    {
                    // if some actions should not be usable, like move, this code must be adopted
                    event->acceptProposedAction();
                    }
                    @

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      hoozi
                      wrote on last edited by
                      #11

                      problem Still ......

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        hoozi
                        wrote on last edited by
                        #12

                        I feel so sorry for you. :-(

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          giesbert
                          wrote on last edited by
                          #13

                          Hi, I looked a bit into this and also on the internet. Seems like in QDeclarativeView, dropping from outside does not work anymore, "see this thread":http://developer.qt.nokia.com/forums/viewthread/913

                          You can have a look at "this code fragements,":http://bytebucket.org/gregschlom/qml-drag-drop/src/0d430d66ceb6 perhaps they help you

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            hoozi
                            wrote on last edited by
                            #14

                            Thanks, Gerolf
                            I run Grégory Schlomoff 's code , it works . but a few problem

                            he Packaged the code to Plugin, I don't understand his code.
                            I email he.

                            Anyway, Thank you for pay lots of time for me.

                            1 Reply Last reply
                            0

                            • Login

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