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

Setting font in QMimedata



  • 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);
    }
    

  • Moderators

    @Andeol

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

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



  • 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?


  • Moderators

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


Log in to reply