March 28, 2012

xzwinglingx xzwinglingx
Lab Rat
3 posts

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:

  1. QTDragAndDrop::QTDragAndDrop(QWidget *parent, Qt::WFlags flags)
  2.  : QMainWindow(parent, flags)
  3. {
  4.  ui.setupUi(this);
  5.  
  6.  //Accept Drops
  7.  setAcceptDrops(true);
  8. }

Thanks for any help ;)

4 replies

March 28, 2012

DerManu DerManu
Robot Herder
451 posts

Did you reimplement

  1. void QWidget::dragEnterEvent ( QDragEnterEvent * event ) [virtual protected]
  2. void QWidget::dropEvent ( QDropEvent * event ) [virtual protected]

properly? For example, you could check for

  1. 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())

March 28, 2012

xzwinglingx xzwinglingx
Lab Rat
3 posts

Jeah i copied the source from a example:

in the Header

  1.  //Drag and Drop Events
  2.  void dragEnterEvent(QDragEnterEvent *event);
  3.  void dropEvent(QDropEvent *event);

Cpp:

  1. void QDirectXWidget::dropEvent( QDropEvent *event )
  2. {
  3.  foreach(QUrl url, event->mimeData()->urls())
  4.  {
  5.   QString filename = url.toLocalFile();
  6.   QString suffix = QFileInfo(filename).suffix().toUpper();
  7.   if(suffix=="PNG")
  8.   {
  9.    event->acceptProposedAction();
  10.    // do something
  11.    continue;
  12.   }
  13.  }
  14. }
  15.  
  16. void QDirectXWidget::dragEnterEvent( QDragEnterEvent *event )
  17. {
  18.  foreach(QUrl url, event->mimeData()->urls())
  19.   if (QFileInfo(url.toLocalFile()).suffix().toUpper()=="PNG")
  20.   {
  21.    event->acceptProposedAction();
  22.    return;
  23.   }
  24. }

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

March 28, 2012

DerManu DerManu
Robot Herder
451 posts

I’ve just created an empty project (QMainWindow subclass), set setAcceptDrops(true) in the constructor and reimplemented

  1. void MainWindow::dragEnterEvent(QDragEnterEvent *event)
  2. {
  3.   qDebug() << "dragEnter" << event->mimeData()->formats();
  4.   event->acceptProposedAction();
  5. }
  6.  
  7. void MainWindow::dropEvent(QDropEvent *event)
  8. {
  9.   qDebug() << "drop" << event->mimeData()->formats();
  10. }

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?

March 28, 2012

xzwinglingx xzwinglingx
Lab Rat
3 posts

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

Thanks for helping^^

 
  ‹‹ [SOLVED] Retrieve the colours used by Qt to render a frame of StyledPanel shape      Sharing data between an offscreen QGLContext and a QGLWidget ››

You must log in to post a reply. Not a member yet? Register here!