Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QClipboard and QMimeData not working for urls



  •     QUrl url = QUrl("http://www.google.com");
        QMimeData *mime = new QMimeData;
        mime->setUrls({ url });
    
        QGuiApplication::clipboard()->setMimeData(mime);
    

    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()) {
              setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
          } else if (mimeData->hasHtml()) {
              setText(mimeData->html());
              setTextFormat(Qt::RichText);
          } else if (mimeData->hasText()) {
              setText(mimeData->text());
              setTextFormat(Qt::PlainText);
          } 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("http://www.google.com");
        QUrl url2 = QUrl("http://www.microsoft.com");
        QMimeData *mime = new QMimeData;
        mime->setUrls({ url1, url2 });
        qDebug() << "urls : " << mime->urls();
    
        QClipboard *clipboard = QApplication::clipboard();
        if(clipboard)
        {
            clipboard->setMimeData(mime);
           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

    @Mr-Gisa
    Hi
    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
    https://www.nirsoft.net/utils/inside_clipboard.html
    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

    @Mr-Gisa
    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