Copy & Paste of custom text objects?



  • How can one enable copy and paste operations of custom text objects in a QTextEditor based on QTextObjectInterface and ObjectReplacementCharacter?

    I know selection, copy/paste, toPlaintext(), toHtml() and similar functions just ignore custom text objects. But if I were to make a radical change (derive and implement my own classes) then where would I begin? What direction shall I look?

    Thanks for any advise.


  • Moderators

    @Ramin
    this is currently not possible: QTBUG-14487
    You have to create the mime data yourself.
    E.g. for QTextEdit you need to reimplement QMimeData * QTextEdit::createMimeDataFromSelection() const

    And replace your replace character with the data in the way you want it to be represented. (in text/plain, text/html ...)



  • @raven-worx Thanks, are there any architectural documents anywhere that I can use to understand the relationships between various classes ?

    What you explain seems like something I can do except you know the big picture and I have no idea where to start!



  • @raven-worx I do see there is a patch on that link, so if I build QT from source with that patch would this enable the copy/paste?



  • @raven-worx I get what you are saying , and if there are no comprehensive documents about the way QTextEdit or related classes work then I guess reading createMimeDataFromSelection() and replacing it is the only way forward. I just want to know if there is a less intrusive way without patching QT.


  • Moderators

    @Ramin said in [QTextObjectInterface] Copy & Paste of custom text objects?:

    I do see there is a patch on that link, so if I build QT from source with that patch would this enable the copy/paste?

    maybe, but seems like this patch is for Qt 4.7. It may be not applicable for Qt 5. In the worst case you have to do conflict resolution.

    @Ramin said in [QTextObjectInterface] Copy & Paste of custom text objects?:

    Thanks, are there any architectural documents anywhere that I can use to understand the relationships between various classes ?
    What you explain seems like something I can do except you know the big picture and I have no idea where to start!

    about what relationships between various classes are you talking? The best resource are the docs for the specific class. They normally explain the relationship pretty well.

    The createMimeDataFromSelection() method is called internally by QTextEdit whenever you press Ctrl+C or copy from the context menu, etc.

    QMimeData * QTextEdit::createMimeDataFromSelection() const
    {
           QMimeData* mimeData = new QMimeData;
           
           QTextCursor c = this->cursor();
           
           QString plainTextStr = c.selectedText();
           // replace 'QChar::ObjectReplacementCharacter' in plainTextStr
           mimeData->setText( plainTextStr );
    
           // html is a bit more tricky: use 'QTextDocumentFragment selection = c.selection()'
    
           return mimeData;
    }
    

  • Moderators

    @Ramin said in [QTextObjectInterface] Copy & Paste of custom text objects?:

    I just want to know if there is a less intrusive way without patching QT.

    actually no.
    When you also want to support pasting of your custom text object you need to define the properties to be able to properly recreate it later on.
    So i doubt that there is a generic way to support this (without providing a proper API).

    On a quick glance on the patch i also just see the HTML export (but maybe this is also used to create the plain-text, i don't know) and no pasting.



  • @raven-worx Thanks for this. I'll dig into it and see where I get stuck!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.