Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QClipboard and QMimeData not working for urls
Qt 6.11 is out! See what's new in the release blog

QClipboard and QMimeData not working for urls

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 3.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Gisa
    wrote on last edited by
    #1
        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.

    K 1 Reply Last reply
    0
    • M Mr Gisa
          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.

      K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      @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"));
            }
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mr Gisa
        wrote on last edited by
        #3

        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.

        K 1 Reply Last reply
        0
        • M Mr Gisa

          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.

          K Offline
          K Offline
          kenchan
          wrote on last edited by
          #4

          @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();
              }
          
          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mr Gisa
            wrote on last edited by
            #5

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

            K 1 Reply Last reply
            0
            • M Mr Gisa

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

              K Offline
              K Offline
              kenchan
              wrote on last edited by
              #6

              @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 :-)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mr Gisa
                wrote on last edited by Mr Gisa
                #7

                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.

                mrjjM 1 Reply Last reply
                0
                • M Mr Gisa

                  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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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.

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    Mr Gisa
                    wrote on last edited by
                    #9

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

                    mrjjM 1 Reply Last reply
                    0
                    • M Mr Gisa

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @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.

                      1 Reply Last reply
                      2
                      • M Offline
                        M Offline
                        Mr Gisa
                        wrote on last edited by
                        #11

                        Thank you @mrjj

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved