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. Write rich text into .odt file
QtWS25 Last Chance

Write rich text into .odt file

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 1.8k Views
  • 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.
  • T Offline
    T Offline
    Tamfub
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      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.

      For more information please re-read.

      Kind Regards,
      Artur

      T 1 Reply Last reply
      0
      • artwawA artwaw

        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.

        T Offline
        T Offline
        Tamfub
        wrote on last edited by
        #3

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

        artwawA SGaistS 2 Replies Last reply
        0
        • T Tamfub

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

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

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

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • T Tamfub

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

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply
            0
            • SGaistS SGaist

              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 ?

              T Offline
              T Offline
              Tamfub
              wrote on last edited by Tamfub
              #6

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

              artwawA 1 Reply Last reply
              0
              • T Tamfub

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

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #7

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

                For more information please re-read.

                Kind Regards,
                Artur

                SGaistS 1 Reply Last reply
                0
                • artwawA artwaw

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

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tamfub
                    wrote on last edited by
                    #9

                    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.

                    artwawA 1 Reply Last reply
                    0
                    • T Tamfub

                      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.

                      artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on last edited by
                      #10

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

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        So you want to implement something like Google Docs ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          Tamfub
                          wrote on last edited by Tamfub
                          #12

                          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?

                          artwawA 1 Reply Last reply
                          0
                          • T Tamfub

                            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?

                            artwawA Offline
                            artwawA Offline
                            artwaw
                            wrote on last edited by
                            #13

                            @Tamfub Of course.

                            For more information please re-read.

                            Kind Regards,
                            Artur

                            T 1 Reply Last reply
                            0
                            • artwawA artwaw

                              @Tamfub Of course.

                              T Offline
                              T Offline
                              Tamfub
                              wrote on last edited by
                              #14

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

                              artwawA 1 Reply Last reply
                              0
                              • T Tamfub

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

                                artwawA Offline
                                artwawA Offline
                                artwaw
                                wrote on last edited by
                                #15

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

                                For more information please re-read.

                                Kind Regards,
                                Artur

                                1 Reply Last reply
                                2
                                • T Offline
                                  T Offline
                                  Tamfub
                                  wrote on last edited by
                                  #16

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

                                  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