[Interest] Starting drag operation from slot.

Igor Mironchik igor.mironchik at gmail.com
Fri Feb 12 13:25:18 CET 2016


Hi folks,

I need to start drag operation in slot connected to QAction::triggered().

Below is what I do in the slot.

{
     const QString fileName =
         QFileDialog::getOpenFileName( this, tr( "Select Image" ),
             QStandardPaths::standardLocations(
                 QStandardPaths::PicturesLocation ).first(),
             tr( "Image Files (*.png *.jpg *.jpeg *.bmp)" ) );

     if( !fileName.isEmpty() )
     {
         QImage image( fileName );

         if( !image.isNull() )
         {
             QDrag * drag = new QDrag( this );
             QMimeData * mimeData = new QMimeData;

             QPixmap p;
             QSize s = image.size();

             if( s.width() > 50 || s.height() > 50 )
             {
                 s = s.boundedTo( QSize( 50, 50 ) );
                 p = QPixmap::fromImage(
                     image.scaled( s, Qt::KeepAspectRatio, 
Qt::SmoothTransformation ) );
             }
             else
                 p = QPixmap::fromImage( image );

             mimeData->setImageData( image );
             drag->setMimeData( mimeData );
             drag->setPixmap( p );

             QApplication::processEvents();

             QMouseEvent event( QEvent::MouseButtonPress,
                 QPointF( -10.0, -10.0 ), Qt::LeftButton, 0, 0 );

             QApplication::sendEvent( this, &event );

             drag->exec();
         }
         else
             QMessageBox::warning( this, tr( "Wrong Image..." ),
                 tr( "Failed to load image from \"%1\"." ).arg( fileName 
) );
     }
}

This works if I choose image and press button OK in file dialog.

But if I double-click on the file name in file dialog then drag appears 
and immediately disappear...

How to fix it?

Any ideas, please.

Thank you.



More information about the Interest mailing list