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. How to use both the properties of Qtextdocument
Forum Updated to NodeBB v4.3 + New Features

How to use both the properties of Qtextdocument

Scheduled Pinned Locked Moved Unsolved General and Desktop
33 Posts 3 Posters 6.1k 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.
  • ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by ManiRon
    #1

    I want to use both setPlainText() and sethtml() property of QTextDocument. But i tried use both but it replaces the later. setHtml() is used for printing the header for my pdf and setplaintext() is used to print the text in the pdf file. is there any way that i can use both and create a PDF. Both the header and the text should be in a single PDF . Its like creating a report.

    JonBJ 1 Reply Last reply
    0
    • ManiRonM ManiRon

      I want to use both setPlainText() and sethtml() property of QTextDocument. But i tried use both but it replaces the later. setHtml() is used for printing the header for my pdf and setplaintext() is used to print the text in the pdf file. is there any way that i can use both and create a PDF. Both the header and the text should be in a single PDF . Its like creating a report.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ManiRon
      No, you cannot use both. setPlainText() and sethtml() both set the whole of the QTextDocument text. The only difference is whether you pass in plain text or HTML.

      ManiRonM 1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by aha_1980
        #3

        You can using the QTextCursor interface:

        QTextCursor cursor(textDocument);
        cursor.insertHtml(/*...*/);
        cursor.insertText(/*...*/);
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        ManiRonM 2 Replies Last reply
        6
        • ManiRonM Offline
          ManiRonM Offline
          ManiRon
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • JonBJ JonB

            @ManiRon
            No, you cannot use both. setPlainText() and sethtml() both set the whole of the QTextDocument text. The only difference is whether you pass in plain text or HTML.

            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by
            #5

            @JonB i use both . Because i use HTML to place my header image and title and the text to display the data.

            1 Reply Last reply
            0
            • VRoninV VRonin

              You can using the QTextCursor interface:

              QTextCursor cursor(textDocument);
              cursor.insertHtml(/*...*/);
              cursor.insertText(/*...*/);
              
              ManiRonM Offline
              ManiRonM Offline
              ManiRon
              wrote on last edited by ManiRon
              #6

              @VRonin how i can use the textcursor to print the data in the PDF File. In QTextDocument i can use the print option but in textcursor what can i use.0_1534920606771_3.JPG

              JonBJ 1 Reply Last reply
              0
              • VRoninV VRonin

                You can using the QTextCursor interface:

                QTextCursor cursor(textDocument);
                cursor.insertHtml(/*...*/);
                cursor.insertText(/*...*/);
                
                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #7

                @VRonin I tried to do this.

                        QTextCursor document1;
                        document1.document()->setHtml(hex);
                        QString data = ui->teData->toPlainText();
                        document1.document()->setPlainText(data);
                

                but i got the above error when i ran with the debug mode . and the error was thrown from document1.document()->setHtml(hex); this line of code .

                JonBJ 1 Reply Last reply
                0
                • ManiRonM ManiRon

                  @VRonin how i can use the textcursor to print the data in the PDF File. In QTextDocument i can use the print option but in textcursor what can i use.0_1534920606771_3.JPG

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @ManiRon

                  i use both .

                  You cannot use both setPlainText() and setHtml() because as I said they each completely replace the whole content of the QTextDocument.

                  You can use @VRonin's QTextCursor interface to insert individual chunks of either text or HTML at specified places within a document. QTextCursor is a "pointer" to a location in the document; you can move it around the document with various calls and then insert at those points.

                  how i can use the textcursor to print the data in the PDF File.

                  Don't know what you mean by "print" here.

                  In QTextDocument i can use the print option but in textcursor what can i use.

                  That is just converting the whole QTextDocument to PDF.

                  It remains the case that you cannot "put PDF into a QTextDocument". You can only insert plain text or HTML.

                  1 Reply Last reply
                  1
                  • ManiRonM ManiRon

                    @VRonin I tried to do this.

                            QTextCursor document1;
                            document1.document()->setHtml(hex);
                            QString data = ui->teData->toPlainText();
                            document1.document()->setPlainText(data);
                    

                    but i got the above error when i ran with the debug mode . and the error was thrown from document1.document()->setHtml(hex); this line of code .

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @ManiRon said in How to use both the properties of Qtextdocument:

                    @VRonin I tried to do this.

                            QTextCursor document1;
                            document1.document()->setHtml(hex);
                            QString data = ui->teData->toPlainText();
                            document1.document()->setPlainText(data);
                    

                    but i got the above error when i ran with the debug mode . and the error was thrown from document1.document()->setHtml(hex); this line of code .

                    1. hex is doubtless some illegal HTML (not HTML at all).

                    2. You persist in trying to use document()->setHtml(...) followed by document()->setPlainText(data). If you do not believe me that you cannot use both (the second will simply replace the first), why don't you read the docs or try a small test for yourself so that you know why it won't work?

                    ManiRonM 4 Replies Last reply
                    2
                    • JonBJ JonB

                      @ManiRon said in How to use both the properties of Qtextdocument:

                      @VRonin I tried to do this.

                              QTextCursor document1;
                              document1.document()->setHtml(hex);
                              QString data = ui->teData->toPlainText();
                              document1.document()->setPlainText(data);
                      

                      but i got the above error when i ran with the debug mode . and the error was thrown from document1.document()->setHtml(hex); this line of code .

                      1. hex is doubtless some illegal HTML (not HTML at all).

                      2. You persist in trying to use document()->setHtml(...) followed by document()->setPlainText(data). If you do not believe me that you cannot use both (the second will simply replace the first), why don't you read the docs or try a small test for yourself so that you know why it won't work?

                      ManiRonM Offline
                      ManiRonM Offline
                      ManiRon
                      wrote on last edited by
                      #10

                      @JonB oh ok got it . but now my problem is i want to add an header to my pdf file and text also . is there any way

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @ManiRon said in How to use both the properties of Qtextdocument:

                        @VRonin I tried to do this.

                                QTextCursor document1;
                                document1.document()->setHtml(hex);
                                QString data = ui->teData->toPlainText();
                                document1.document()->setPlainText(data);
                        

                        but i got the above error when i ran with the debug mode . and the error was thrown from document1.document()->setHtml(hex); this line of code .

                        1. hex is doubtless some illegal HTML (not HTML at all).

                        2. You persist in trying to use document()->setHtml(...) followed by document()->setPlainText(data). If you do not believe me that you cannot use both (the second will simply replace the first), why don't you read the docs or try a small test for yourself so that you know why it won't work?

                        ManiRonM Offline
                        ManiRonM Offline
                        ManiRon
                        wrote on last edited by
                        #11

                        @JonB hex is a QString variable. In which i store the html code read from a file.

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @ManiRon said in How to use both the properties of Qtextdocument:

                          @VRonin I tried to do this.

                                  QTextCursor document1;
                                  document1.document()->setHtml(hex);
                                  QString data = ui->teData->toPlainText();
                                  document1.document()->setPlainText(data);
                          

                          but i got the above error when i ran with the debug mode . and the error was thrown from document1.document()->setHtml(hex); this line of code .

                          1. hex is doubtless some illegal HTML (not HTML at all).

                          2. You persist in trying to use document()->setHtml(...) followed by document()->setPlainText(data). If you do not believe me that you cannot use both (the second will simply replace the first), why don't you read the docs or try a small test for yourself so that you know why it won't work?

                          ManiRonM Offline
                          ManiRonM Offline
                          ManiRon
                          wrote on last edited by ManiRon
                          #12

                          @JonB header has top left corner one image and top right corner one image and a title at the middle of the two images, and after that i want insert my texts. This much things i want to do . Is there any way ? :(

                          VRoninV JonBJ 2 Replies Last reply
                          0
                          • ManiRonM ManiRon

                            @JonB header has top left corner one image and top right corner one image and a title at the middle of the two images, and after that i want insert my texts. This much things i want to do . Is there any way ? :(

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            @ManiRon Take a look at my code above. Look at what I pass in the constructor of QTextCursor and look what methods I'm calling. now look at your code and notice the differences

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            ManiRonM 1 Reply Last reply
                            2
                            • VRoninV VRonin

                              @ManiRon Take a look at my code above. Look at what I pass in the constructor of QTextCursor and look what methods I'm calling. now look at your code and notice the differences

                              ManiRonM Offline
                              ManiRonM Offline
                              ManiRon
                              wrote on last edited by ManiRon
                              #14

                              @VRonin i tried the same way as yours (QTextCursor cursor(textDocument);) but it says

                              mainwindow.cpp:107: error: 'textDocument' was not declared in this scope
                              QTextCursor cursor(textDocument);
                              ^

                              VRoninV JonBJ 2 Replies Last reply
                              0
                              • JonBJ JonB

                                @ManiRon said in How to use both the properties of Qtextdocument:

                                @VRonin I tried to do this.

                                        QTextCursor document1;
                                        document1.document()->setHtml(hex);
                                        QString data = ui->teData->toPlainText();
                                        document1.document()->setPlainText(data);
                                

                                but i got the above error when i ran with the debug mode . and the error was thrown from document1.document()->setHtml(hex); this line of code .

                                1. hex is doubtless some illegal HTML (not HTML at all).

                                2. You persist in trying to use document()->setHtml(...) followed by document()->setPlainText(data). If you do not believe me that you cannot use both (the second will simply replace the first), why don't you read the docs or try a small test for yourself so that you know why it won't work?

                                ManiRonM Offline
                                ManiRonM Offline
                                ManiRon
                                wrote on last edited by ManiRon
                                #15

                                @JonB I agree with u sir. But i am in a situation that i require to use both . Thats why keep on asking about it. Dont Mistake.0_1534921806909_4.JPG . It shows like this.

                                ManiRonM 1 Reply Last reply
                                0
                                • ManiRonM ManiRon

                                  @VRonin i tried the same way as yours (QTextCursor cursor(textDocument);) but it says

                                  mainwindow.cpp:107: error: 'textDocument' was not declared in this scope
                                  QTextCursor cursor(textDocument);
                                  ^

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #16

                                  @ManiRon The title of your post is "How to use both the properties of Qtextdocument" I assumed you have a QTextdocument to work on. maybe ui->teData->document()?

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  1 Reply Last reply
                                  0
                                  • ManiRonM ManiRon

                                    @JonB I agree with u sir. But i am in a situation that i require to use both . Thats why keep on asking about it. Dont Mistake.0_1534921806909_4.JPG . It shows like this.

                                    ManiRonM Offline
                                    ManiRonM Offline
                                    ManiRon
                                    wrote on last edited by
                                    #17

                                    @ManiRon 0_1534921905213_5.JPG

                                    1 Reply Last reply
                                    0
                                    • ManiRonM ManiRon

                                      @VRonin i tried the same way as yours (QTextCursor cursor(textDocument);) but it says

                                      mainwindow.cpp:107: error: 'textDocument' was not declared in this scope
                                      QTextCursor cursor(textDocument);
                                      ^

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by JonB
                                      #18

                                      @ManiRon said in How to use both the properties of Qtextdocument:

                                      @VRonin i tried the same way as yours (QTextCursor cursor(textDocument);) but it says

                                      mainwindow.cpp:107: error: 'textDocument' was not declared in this scope
                                      QTextCursor cursor(textDocument);
                                      ^

                                      The error message tells you exactly what is wrong with your code, nothing to add by showing screenshots. You cannot expect to copy @VRonin's suggestion verbatim without any attempt to understand what it is doing and what you need to do to complete it. Why don't you have a look in the docs at QTextCursor's constructors? He wants you to understand what is wrong and deal with it, for your own long-term benefit.

                                      ManiRonM 1 Reply Last reply
                                      2
                                      • JonBJ JonB

                                        @ManiRon said in How to use both the properties of Qtextdocument:

                                        @VRonin i tried the same way as yours (QTextCursor cursor(textDocument);) but it says

                                        mainwindow.cpp:107: error: 'textDocument' was not declared in this scope
                                        QTextCursor cursor(textDocument);
                                        ^

                                        The error message tells you exactly what is wrong with your code, nothing to add by showing screenshots. You cannot expect to copy @VRonin's suggestion verbatim without any attempt to understand what it is doing and what you need to do to complete it. Why don't you have a look in the docs at QTextCursor's constructors? He wants you to understand what is wrong and deal with it, for your own long-term benefit.

                                        ManiRonM Offline
                                        ManiRonM Offline
                                        ManiRon
                                        wrote on last edited by
                                        #19

                                        @JonB Yes sir i am getting that i cannot use both at the same time and i tried with a sample code too.

                                        1 Reply Last reply
                                        0
                                        • ManiRonM ManiRon

                                          @JonB header has top left corner one image and top right corner one image and a title at the middle of the two images, and after that i want insert my texts. This much things i want to do . Is there any way ? :(

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by
                                          #20

                                          @ManiRon said in How to use both the properties of Qtextdocument:

                                          @JonB header has top left corner one image and top right corner one image and a title at the middle of the two images, and after that i want insert my texts. This much things i want to do . Is there any way ? :(

                                          Assuming this has nothing to do with PDF (sometimes you talk about the PDF header or "inserting PDF", I'm never sure), this sounds like you want to insert an image into your document? Have a read of http://doc.qt.io/qt-5/richtext-cursor.html and http://doc.qt.io/qt-5/qtextcursor.html#insertImage-2 ? I really think you need to do some reading of this sort of stuff to get an idea of what you can do with QTextDocument & QTextCursor.

                                          ManiRonM 2 Replies 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