Can't get Drag and Drop to Work:



  • 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 ;)



  • 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())



  • 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



  • 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?



  • 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^^


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.