Important: Please read the Qt Code of Conduct -

QClipboard and QMimeData not working for urls

  •     QUrl url = QUrl("");
        QMimeData *mime = new QMimeData;
        mime->setUrls({ url });

    It's not working, the url is not being copied to Clipboard. The clipboard works if I use something like setText but not this way.

  • @Mr-Gisa
    I guess that is how it is supposed to work..
    Like it says in the QClipboard docs...

      void DropArea::paste()
          const QClipboard *clipboard = QApplication::clipboard();
          const QMimeData *mimeData = clipboard->mimeData();
          if (mimeData->hasImage()) {
          } else if (mimeData->hasHtml()) {
          } else if (mimeData->hasText()) {
          } else {
              setText(tr("Cannot display data"));

  • Yes, but it's not working, the urls are not being copied to the Clipboard. It only works if I use setText and QUrl::toString, using mime and setMime isn't working.

  • @Mr-Gisa

    Well that is strange, because when I do something like this in my application I can get the urls back from the clipboard.
    So I guess it does work and you are doing something wrong...

        QUrl url1 = QUrl("");
        QUrl url2 = QUrl("");
        QMimeData *mime = new QMimeData;
        mime->setUrls({ url1, url2 });
        qDebug() << "urls : " << mime->urls();
        QClipboard *clipboard = QApplication::clipboard();
           const QMimeData *clipMime = clipboard->mimeData();
           qDebug() << "mime urls : " << clipMime->urls();

  • Have you tried copying to Clipboard and using ctrl+v on something like notepad? For me doesn't work, idk why.

  • @Mr-Gisa
    Nope, could not paste it into a text editor but I could paste into the url text box on the google chrome browser and the Safari browser...
    Maybe that is because it is a url mime type and not a text type? However, I must admit I am not an expert on mime type behaviour :-)

  • For me I didn't work even on the browser (Firefox), I did the same qDebug thing you did and it's returning the urls, but not copying to the clipboard actually. That is so weird.

    I noticed that I can only paste on QLineEdit inside my application, I can't paste in other applications.

  • Lifetime Qt Champion

    Just as a note:
    On windows, most apps checks the format put in clipboard with the data.
    So notepad for instance, wont paste types besides TEXT etc.
    you could try with
    and see what it shows.

  • Is there a problem changing from using mime data and using just setText for urls or it's going to be a problem?

  • Lifetime Qt Champion

    No, not really as url works as text too.
    In theory, the URL could contain special characters and languages but
    the text format should also handle that nowadays.

    Also text format is often best for pasting to externals app.
    Some do check mimetypes and can paste many formats but many app
    like TEXT and IMAGE and ignores the rest. ( a bit oversimplified)

    So i think in your use case, text format will work fine.

  • Thank you @mrjj

Log in to reply