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 create a valid and "usable" QTextFrameFormat
Forum Updated to NodeBB v4.3 + New Features

How to create a valid and "usable" QTextFrameFormat

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 4.4k Views 3 Watching
  • 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.
  • N Offline
    N Offline
    Nixxer
    wrote on last edited by
    #1

    Hi!

    I was experimenting with the options for formatting text and frames, but i can't make a valid QTextFrameFormat...

    I have tried to make this function:

    void MainWindow::setDoc()
    {
        QTextDocument doc(this);
        QTextFrame frame1(&doc);
        QTextCursor cursor1 = frame1.firstCursorPosition();
        QTextFrameFormat format1;
    
        cursor1.insertText("Some text.\nOther text.");
    
        format1.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
        format1.setBorder(0.5);
        format1.setPosition(QTextFrameFormat::InFlow);
    
        qDebug() << format1.isValid();
    
        if(format1.isValid())
        {
            frame1.setFrameFormat(format1);
        }
    }
    

    When i call this function, the program crashes, reporting this error "ASSERT failure in QVector<T>::operator[]: "index out of range", file ....\include/QtCore/../../src/corelib/tools/qvector.h, line 401".

    I have also tried with the function isValid() to verify the validity of the format, but it returns true...
    So, am i missing some important options? Or am i applying the method in the wrong way?

    Thanks in advance for the help!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Nixxer said:

      QTextFrameFormat

      Hi never used it so no expert, but I was wondering if you looked at
      https://doc.qt.io/archives/4.6/richtext-orderform.html

      as it uses a QTextTableFormat for its order element.

      N 1 Reply Last reply
      0
      • mrjjM mrjj

        @Nixxer said:

        QTextFrameFormat

        Hi never used it so no expert, but I was wondering if you looked at
        https://doc.qt.io/archives/4.6/richtext-orderform.html

        as it uses a QTextTableFormat for its order element.

        N Offline
        N Offline
        Nixxer
        wrote on last edited by
        #3

        @mrjj
        Well, thanks for the reply, but i took a look at the page, but i see that QTextTableFormat is for a table, so i don't think it's the right tool for my intent...

        Moreover, i see this code:

          TextFrameFormat topFrameFormat = topFrame->frameFormat();
         topFrameFormat.setPadding(16);
         topFrame->setFrameFormat(topFrameFormat);
        

        It reminds me my code, but maybe i'm missing something...

        mrjjM 1 Reply Last reply
        0
        • N Nixxer

          @mrjj
          Well, thanks for the reply, but i took a look at the page, but i see that QTextTableFormat is for a table, so i don't think it's the right tool for my intent...

          Moreover, i see this code:

            TextFrameFormat topFrameFormat = topFrame->frameFormat();
           topFrameFormat.setPadding(16);
           topFrame->setFrameFormat(topFrameFormat);
          

          It reminds me my code, but maybe i'm missing something...

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Nixxer
          hi
          well it also uses a QTextFrameFormat..
          I agree that your code looks very alike. except maybe how to get the cursor.

          Did you try to single step it and see what line it crashes on?

          N 1 Reply Last reply
          0
          • mrjjM mrjj

            @Nixxer
            hi
            well it also uses a QTextFrameFormat..
            I agree that your code looks very alike. except maybe how to get the cursor.

            Did you try to single step it and see what line it crashes on?

            N Offline
            N Offline
            Nixxer
            wrote on last edited by
            #5

            @mrjj
            I have tried to put a breakpoint in every line of the function, it works until the last line, the one that applies the QTextFramwFormat to the QTextFrame...

            I have also tried to get the current format (prior my changes) with format1 = frame1.frameFormat(), to make sure to start with a "sensible" format, but it didn't work anyway...

            mrjjM 1 Reply Last reply
            0
            • N Nixxer

              @mrjj
              I have tried to put a breakpoint in every line of the function, it works until the last line, the one that applies the QTextFramwFormat to the QTextFrame...

              I have also tried to get the current format (prior my changes) with format1 = frame1.frameFormat(), to make sure to start with a "sensible" format, but it didn't work anyway...

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Nixxer
              hmm so it dont like format1.
              Only thing that spring to mind is the cursor thing. but firstCursorPosition should be fine too?

              If we look at sample code:

              QTextEdit *editor = new QTextEdit;
              QTextCursor cursor(editor->textCursor());
              cursor.movePosition(QTextCursor::Start);
              QTextFrame *topFrame = cursor.currentFrame();
              QTextFrameFormat topFrameFormat = topFrame->frameFormat();
              topFrameFormat.setPadding(16);
              topFrame->setFrameFormat(topFrameFormat);
              ...
              QTextCharFormat boldFormat;
              boldFormat.setFontWeight(QFont::Bold);
              cursor.insertText("A company", boldFormat);
              

              It looks like yours alot.

              N 1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                One difference:

                QTextFrameFormat topFrameFormat = topFrame->frameFormat();

                where your code just declare a new one.
                QTextFrameFormat format1;

                So I wonder if its need more "setup"

                1 Reply Last reply
                0
                • mrjjM mrjj

                  @Nixxer
                  hmm so it dont like format1.
                  Only thing that spring to mind is the cursor thing. but firstCursorPosition should be fine too?

                  If we look at sample code:

                  QTextEdit *editor = new QTextEdit;
                  QTextCursor cursor(editor->textCursor());
                  cursor.movePosition(QTextCursor::Start);
                  QTextFrame *topFrame = cursor.currentFrame();
                  QTextFrameFormat topFrameFormat = topFrame->frameFormat();
                  topFrameFormat.setPadding(16);
                  topFrame->setFrameFormat(topFrameFormat);
                  ...
                  QTextCharFormat boldFormat;
                  boldFormat.setFontWeight(QFont::Bold);
                  cursor.insertText("A company", boldFormat);
                  

                  It looks like yours alot.

                  N Offline
                  N Offline
                  Nixxer
                  wrote on last edited by
                  #8

                  @mrjj
                  One thing that differs is that i declare all my variables in the stack, while the example declares them in the heap, but i hope that all that works in the heap should work also in the stack and vice-versa...

                  My intention was to make a QTextDocument, put in this a QTextFrame, put a text in it with a QTextCursor, create a QTextFrameFormat to be applied to the QTextFrame...

                  mrjjM 1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    minimal sample that works for me

                    QTextEdit *editor = new QTextEdit;
                    QTextCursor cursor(editor->textCursor());
                    cursor.movePosition(QTextCursor::Start);
                    QTextFrame *topFrame = cursor.currentFrame();
                    QTextFrameFormat topFrameFormat = topFrame->frameFormat();
                    topFrameFormat.setPadding(16);
                    topFrame->setFrameFormat(topFrameFormat);
                    cursor.insertBlock();
                    cursor.insertText("321 City Street");

                    1 Reply Last reply
                    0
                    • N Nixxer

                      @mrjj
                      One thing that differs is that i declare all my variables in the stack, while the example declares them in the heap, but i hope that all that works in the heap should work also in the stack and vice-versa...

                      My intention was to make a QTextDocument, put in this a QTextFrame, put a text in it with a QTextCursor, create a QTextFrameFormat to be applied to the QTextFrame...

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @Nixxer
                      Well it crashes before you run out of scope so I doubt it is related to the stack :)

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

                        @Nixxer said:
                        One thing I wondered:
                        The sample does not new / create a QTextDocument as far as I can see.
                        I wonder if it is needed to do , or I simply don't understand what you want to do :)

                        N 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @Nixxer said:
                          One thing I wondered:
                          The sample does not new / create a QTextDocument as far as I can see.
                          I wonder if it is needed to do , or I simply don't understand what you want to do :)

                          N Offline
                          N Offline
                          Nixxer
                          wrote on last edited by
                          #12

                          @mrjj
                          I'll try this snippet, maybe I'll learn something...

                          I begin with the QTextDocument because I'd like to make a program to make documents, like a list of properties of a client, so I thought that I could make a QTextDocument in which inserting one after one these properties with different frames and their ...

                          Maybe I took the problem by the wrong way...

                          Btw, I was thinking to make this program as a exercise to begin to "mess" with Qt...

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

                            Hi,

                            In that case, I'd recommend the excellent "C++ GUI Programming with Qt 4 (2nd Edition)" from Mark Summerfield and Jasmin Blanchette. Although it's Qt 4, it still relevant for Qt 5 and there's a good chapter about QTextDocument

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

                            N 1 Reply Last reply
                            0
                            • N Nixxer

                              @mrjj
                              I'll try this snippet, maybe I'll learn something...

                              I begin with the QTextDocument because I'd like to make a program to make documents, like a list of properties of a client, so I thought that I could make a QTextDocument in which inserting one after one these properties with different frames and their ...

                              Maybe I took the problem by the wrong way...

                              Btw, I was thinking to make this program as a exercise to begin to "mess" with Qt...

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Nixxer
                              Like a database with clients and their properties ?
                              Depending on your coding skills, construction a RTF doc on the fly is maybe a somewhat hardcore start :)

                              N 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                Hi,

                                In that case, I'd recommend the excellent "C++ GUI Programming with Qt 4 (2nd Edition)" from Mark Summerfield and Jasmin Blanchette. Although it's Qt 4, it still relevant for Qt 5 and there's a good chapter about QTextDocument

                                N Offline
                                N Offline
                                Nixxer
                                wrote on last edited by
                                #15

                                @SGaist
                                Thanks for the suggestion! If I can learn something for Qt 5, I'll gladly read this book...

                                1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @Nixxer
                                  Like a database with clients and their properties ?
                                  Depending on your coding skills, construction a RTF doc on the fly is maybe a somewhat hardcore start :)

                                  N Offline
                                  N Offline
                                  Nixxer
                                  wrote on last edited by
                                  #16

                                  @mrjj
                                  It's right, but I prefer to learn starting from "real" problems (I'm an self-taught "programmer")...

                                  mrjjM 1 Reply Last reply
                                  0
                                  • N Nixxer

                                    @mrjj
                                    It's right, but I prefer to learn starting from "real" problems (I'm an self-taught "programmer")...

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @Nixxer
                                    Well thats really understandable. More fun when it's real.
                                    I can only second SGaist book suggestion.
                                    I found it free on
                                    http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf
                                    (takes a moment to load)

                                    Good luck

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved