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. Setting font in QMimedata

Setting font in QMimedata

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 480 Views 1 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.
  • AndeolA Offline
    AndeolA Offline
    Andeol
    wrote on last edited by
    #1

    I want users of my application to be able to copy some text to clipboard, and to paste it to another application of their choice (bloc-note, wordpad, whatever app accept text).
    So I'm using a QMimedata, I set the text in it, and set the mimedata to the QGuiApplication clipboard. No problem so far.
    But ideally, I would like to set the font as well. Is it possible to set a font data to the QMimedata, so that the receiving application keep the font required in the copy ?

    My code is currently along the lines of:

    void copyToClipBoard(QString text)
    {
        QMimeData *mimeData = new QMimeData();
        mimeData->setData("text/plain", QByteArray(textData.toStdString().c_str()));
       // mimeData ->setData("font", "courier") ;  something like this ?
    
        QClipboard* clipboard = QGuiApplication::clipboard();
        clipboard->setMimeData(mimeData);
    }
    

    Developer for R++ : https://rplusplus.com/

    raven-worxR 1 Reply Last reply
    0
    • AndeolA Andeol

      I want users of my application to be able to copy some text to clipboard, and to paste it to another application of their choice (bloc-note, wordpad, whatever app accept text).
      So I'm using a QMimedata, I set the text in it, and set the mimedata to the QGuiApplication clipboard. No problem so far.
      But ideally, I would like to set the font as well. Is it possible to set a font data to the QMimedata, so that the receiving application keep the font required in the copy ?

      My code is currently along the lines of:

      void copyToClipBoard(QString text)
      {
          QMimeData *mimeData = new QMimeData();
          mimeData->setData("text/plain", QByteArray(textData.toStdString().c_str()));
         // mimeData ->setData("font", "courier") ;  something like this ?
      
          QClipboard* clipboard = QGuiApplication::clipboard();
          clipboard->setMimeData(mimeData);
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Andeol

      mimeData ->setData("text/my-font", myQFont.toString().toUtf8())
      

      and use QFont::fromString() on the receiving side

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • AndeolA Offline
        AndeolA Offline
        Andeol
        wrote on last edited by Andeol
        #3

        Thank you for your answer.
        That looks good, but I'm not the one on the receiving side. The idea here is that the receiving side should be other already existing softwares, like Open Office, for example.

        If I copy a text from Wordpad to Open Office, for example, the font is kept, so it's certain that open office gets that info somewhere. But if I copy it from my Qt program using the code you suggested, and copy it into Open Office, the font is lost and replaced by the default. So wordpad manages something my program fails to do.

        Is it just a matter of using the right keyword as key for setData? If so, is there anywhere that documents all the most commonly used one?

        Developer for R++ : https://rplusplus.com/

        raven-worxR 1 Reply Last reply
        0
        • AndeolA Andeol

          Thank you for your answer.
          That looks good, but I'm not the one on the receiving side. The idea here is that the receiving side should be other already existing softwares, like Open Office, for example.

          If I copy a text from Wordpad to Open Office, for example, the font is kept, so it's certain that open office gets that info somewhere. But if I copy it from my Qt program using the code you suggested, and copy it into Open Office, the font is lost and replaced by the default. So wordpad manages something my program fails to do.

          Is it just a matter of using the right keyword as key for setData? If so, is there anywhere that documents all the most commonly used one?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Andeol said in Setting font in QMimedata:

          If I copy a text from Wordpad to Open Office, for example, the font is kept, so it's certain that open office gets that info somewhere.

          ok, then copy text from those programs and use a clipboard inspector to check what mime-data they provide. maybe they simply provide rich text with style information.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          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