How to manage clipboard to check for url using mime-types
-
I tried this, but it didn't work
@ QClipboard *clipboard = QApplication::clipboard();
if(clipboard->mimeData()->hasUrls())
surl=clipboard->text(QClipboard::Clipboard);@ -
You have to use the correct methods, then it works:
@
QClipboard clipboard = QApplication::clipboard();
QMimeData pMime = clipboard->mimeData();if(pMime->hasUrls()) { QUrl url; foreach(url, pMime->urls()) { // do something with the url } }
@
see the "docs of QMimeData":http://qt-project.org/doc/qt-4.8/qmimedata.html#details
-
Why to use foreach, as clipboard can contain only one string (url) at any instant
-
if you select several files for example, you can have several urls in the clipboard
-
I copied http://qt-project.org, then i executed the code:
@QClipboard clipboard = QApplication::clipboard();
const QMimeData pMime = clipboard->mimeData();if(pMime->hasUrls()) { surl=clipboard->text(QClipboard::Clipboard); }@
surl is still empty.
-
if you copy text, then perhaps pMime->hasUrls returns false?
Did you try hasText? -
In that case it would copy any text string, strings which are not urls too. It defeats the purpose of using mime data in that case.
I actually wish to monitor http/ftp download urls copied to clip board, and later on filter out urls of webpages -
The problem here is that it depends on the application thatcopies the text ton the clipborad which type the clipboard data has. If it copies it as text, you can only read it as text. But you could use QUrl to verify, whether it is a url.
To check the mime data, you could use "one of the drag/drop examples":http://qt-project.org/doc/qt-4.8/draganddrop-dropsite.html