BBE:WS

Overland

Using QMime to Create Explorer Files in Qt

Last updated on: 11/3/2013 7:57 PM 
Created on: 11/2/2016 2:22 PM 

Whether it is copy and paste or drag and drop, the applications and the underlying operating system are sharing data by encapsulating content inside MIME. If you are providing drag and drop from a Qt application to the Windows desktop or Explorer Window, then you not only need to wrap your file contents in MIME but also add the proper MIME headers to have Microsoft recognize it. The code below is a sample of the MIME headers and content required to create a desktop shortcut to the Gaspar.net website.


QPoint hotSpot = e->pos();
QList<QUrl> strList;
FILEGROUPDESCRIPTOR fgd;
FILEDESCRIPTOR fd[ 1 ];

strList.append( QUrl( "http://www.gaspar.net/" ) );
fgd.cItems = 1;

fgd.fgd[ 0 ].dwFlags = FD_PROGRESSUI;
wcscpy( fgd.fgd[ 0 ].cFileName, L"Gaspar Website.url" );
QMimeData *mimeData = new QMimeData;
mimeData->setText("www.gaspar.net");
mimeData->setUrls( strList );
mimeData->setData("text/plain", QByteArray( "http://www.gaspar.net" ) );
mimeData->setData("text/html", QByteArray( "<!--StartFragment--><a href=\"http://www.gaspar.net\">The Gaspar Website</a><!--EndFragment-->" ) );
mimeData->setData("FileName", QByteArray( "Gaspar Website.url" ) );
mimeData->setData("FileGroupDescriptorW", QByteArray::fromRawData( ( const char* )&fgd, sizeof ( FILEGROUPDESCRIPTOR ) ) );
mimeData->setData("FileContents", QByteArray( "[InternetShortcut]\r\nURL=http://www.gaspar.net" ) );

QPixmap pixmap(":/images/imagelists/16x16/webpage.png");
render(&pixmap);

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(hotSpot);

Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);

if (dropAction == Qt::MoveAction)
{
    update();
}

Wolffy's Over The Edge

Power of Progress Back to School Randomness What In the World Happened To You? 8th Grader Game Coder When Hobbies Take Over Blackout: The Great Disconnect Different Is Awesome