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

Write rich text into .odt file



  • I am looking for a way to write rich text into a .odt file, starting from a list of Symbol objects. A Symbol is an object of a Symbol class defined by me. At the moment, it contains:

    • the character to be written (QChar)

    • the font style for that character (QFont, i.e. bold, italic...)

    I examined the example project named Text Edit provided in QT Creator. In this example, when the user chooses to save the file, the content of the textEdit element is taken with textEdit->document() (textedit.cpp, line 472) and put in a .odt file.

    textEdit->document() returns the underlying document of textEdit. This is not my case, since I do not have any textEdit element in my UI, so I am looking for a possible way to build this object starting from a character and the relative font.

    QTextDocument *td = new QTextDocument;

    QList<Symbol> symbols;
    /*
     * Example of symbols content:
     * symbols[0] = Symbol('H', QFont::Bold)
     * symbols[1] = Symbol('e', QFont::Bold)
     * symbols[2] = Symbol('l', QFont::Bold)
     * symbols[3] = Symbol('l', QFont::Bold)
     * symbols[4] = Symbol('o', QFont::Bold)
     * symbols[5] = Symbol(',', QFont::Normal)
     * symbols[6] = Symbol(' ', QFont::Normal)
     * symbols[7] = Symbol('W', QFont::Italic)
     * symbols[8] = Symbol('o', QFont::Italic)
     * symbols[9] = Symbol('r', QFont::Italic)
     * symbols[10] = Symbol('l', QFont::Italic)
     * symbols[11] = Symbol('d', QFont::Italic)
     *
     * The Symbol class comes with a getter for both the character and font
     */
    
    for(auto i : symbols){
        // Get the Symbol character and font and do something on td
    }
    
    QTextDocumentWriter writer("savefile.odt");
    bool success = writer.write(td);
    if (success) {
        // OK
    } else {
        // ERROR
    }
    

    If my issue is not clear enough, feel free to ask for explanations! :)



  • Hi,
    as for holding/editing the rich text, you might want to think of QTextDocument class. There really is no need to reinvent the wheel, I think. QTextEdit, on the other hand, is perfect as rich text editor, I'd strongly advice to reconsider the UI design in that regard.



  • @artwaw
    Hi,
    Unfortunately there cannot be a textEdit in my project, so I was wondering if I could do the same with the bits that make the QTextDocument and put them inside it.



  • @Tamfub well, in theory you could, of course. Actual implementation would, of course, depend on which bits of functionality from QTextDocument you'd like to copy...
    However, since the problem lies within UI, I'd stick to keeping QTextDocument having your data. It would be easier, I think, this way as you can save formatted text directly.
    Actual implementation would then rather focus on manipulating the text (insert/edit/delete) and formatting it inside QTextDocument (and there is plethora of classes and methods to help you - all nicely described in the documentation). So you'd have to implement that actual functionality of interacting between whatever UI you have and the data in QTextDocument and not dive into details of saving in the right format.


  • Lifetime Qt Champion

    Hi,

    @Tamfub said in Write rich text into .odt file:

    @artwaw
    Hi,
    Unfortunately there cannot be a textEdit in my project, so I was wondering if I could do the same with the bits that make the QTextDocument and put them inside it.

    Out of curiosity, can you explain the reasons you can't have a QTextEdit in your UI ?



  • @SGaist
    Hello,
    the project I am working on is something like this:
    https://conclave-team.github.io/conclave-site/
    The client and server send data with QTcpSockets. The server side does not have a GUI (it is on the client side, with a textEdit), the client sends each Symbol and the server keeps them in a QList.
    Now, we would like to store this Symbols (that compose a rich text) in a local .odt file.
    I think it is unconvenient to send the whole textEdit->document() at every change inside the client textEdit.



  • @Tamfub I'd still stick with internal representation as QTextEdit, you can edit it as the symbols flow in.


  • Lifetime Qt Champion

    @artwaw said in Write rich text into .odt file:

    @Tamfub I'd still stick with internal representation as QTextEdit, you can edit it as the symbols flow in.

    Agreed, establish a protocol to keep the documents in sync but there's no need to reinvent the wheel.



  • Ok, I have a distributed application and I must apply the CRDT algorithm to keep consistency in shared documents. Since more than one clients may work on the same document (like in Google Docs), how can I keep more than one textEdit graphical element on the server? Maybe I'm not getting it at all.



  • @Tamfub you don't need QTextEdit on the server. Just QTextDocument that's being kept up to date.


  • Lifetime Qt Champion

    So you want to implement something like Google Docs ?



  • Yes, something like that. But if I have a Symbol like one of the first post:

    Symbol s('H', QFont::Bold)
    

    can I create an empty QTextDocument and insert the Symbol character and properties inside it?



  • @Tamfub Of course.



  • @artwaw ok, how can I insert the character? Should I create a QTextBlock inside the QTextDocument or what?



  • @Tamfub You edit QTextDocument using QTextCursor (it is mentioned in QTextDocument documentation btw.).
    Also worth reading before you begin is Rich Text Editing overview page.

    It is a fair amount of reading but in my honest opinion - it is worth it.



  • Fine, I'll give it a read then. Thank you both @artwaw and @SGaist! :)


Log in to reply