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. Can't get Drag and Drop to Work:
Forum Updated to NodeBB v4.3 + New Features

Can't get Drag and Drop to Work:

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 7.7k 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.
  • X Offline
    X Offline
    xzwinglingx
    wrote on last edited by
    #1

    I am playing around with a programming a little GUI Application. I am using the Visual Studio Add-In do design the UI and c++ for the programming. I just want to drop for example a picture from the desktop to the Application and receive the filename with the QUrl class, but when i try do drop it an "forbidden" Icon appears as courser and when i release the mouse button nothing happens.
    I looked in many examples and it seems to me that you only have to setAcceptDrops(true) and it should at least accept the Dropping.

    QT Version is 4.7

    The constructor of my QMainWindow Class:

    @
    QTDragAndDrop::QTDragAndDrop(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    ui.setupUi(this);

    //Accept Drops
    setAcceptDrops(true);
    }
    @

    Thanks for any help ;)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      Did you reimplement
      @void QWidget::dragEnterEvent ( QDragEnterEvent * event ) [virtual protected]
      void QWidget::dropEvent ( QDropEvent * event ) [virtual protected]@

      properly? For example, you could check for
      @event->mimeData()->hasFormat("text/uri-list")@

      when you want to receive file drops from the system's file manager, and accept the event when it returns true. (see event->acceptProposedAction())

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xzwinglingx
        wrote on last edited by
        #3

        Jeah i copied the source from a example:

        in the Header
        @
        //Drag and Drop Events
        void dragEnterEvent(QDragEnterEvent *event);
        void dropEvent(QDropEvent *event);
        @

        Cpp:
        @
        void QDirectXWidget::dropEvent( QDropEvent *event )
        {
        foreach(QUrl url, event->mimeData()->urls())
        {
        QString filename = url.toLocalFile();
        QString suffix = QFileInfo(filename).suffix().toUpper();
        if(suffix=="PNG")
        {
        event->acceptProposedAction();
        // do something
        continue;
        }
        }
        }

        void QDirectXWidget::dragEnterEvent( QDragEnterEvent *event )
        {
        foreach(QUrl url, event->mimeData()->urls())
        if (QFileInfo(url.toLocalFile()).suffix().toUpper()=="PNG")
        {
        event->acceptProposedAction();
        return;
        }
        }
        @

        I set a breakpoint at the loop but the functions don't get called.
        But why isn't there the symbol for dropping when i drag it over the application. Seems wired

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

          I've just created an empty project (QMainWindow subclass), set setAcceptDrops(true) in the constructor and reimplemented
          @void MainWindow::dragEnterEvent(QDragEnterEvent *event)
          {
          qDebug() << "dragEnter" << event->mimeData()->formats();
          event->acceptProposedAction();
          }

          void MainWindow::dropEvent(QDropEvent *event)
          {
          qDebug() << "drop" << event->mimeData()->formats();
          }@

          And as expected it prints the following when drag/dropping items from my file manager:

          dragEnter ("x-special/gnome-icon-list", "text/uri-list", "UTF8_STRING", "text/plain", "COMPOUND_TEXT", "TEXT", "STRING", "text/plain;charset=utf-8")

          drop ("x-special/gnome-icon-list", "text/uri-list", "UTF8_STRING", "text/plain", "COMPOUND_TEXT", "TEXT", "STRING", "text/plain;charset=utf-8")

          Are you sure QDirectXWidget inherits from QWidget and your dragEnterEvent/dropEvent are in the protected section in the header and have correct signature etc?

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xzwinglingx
            wrote on last edited by
            #5

            I also created a new project it now it works too. Properly it was because i always got an Error code "could nit initialize OLE(error 80010106)".

            Thanks for helping^^

            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