How to create a valid and "usable" QTextFrameFormat



  • 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!


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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


  • Qt Champions 2016

    One difference:

    QTextFrameFormat topFrameFormat = topFrame->frameFormat();

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

    So I wonder if its need more "setup"



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


  • Qt Champions 2016

    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");


  • Qt Champions 2016

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


  • Qt Champions 2016

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



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


  • Lifetime Qt Champion

    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


  • Qt Champions 2016

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



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



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


  • Qt Champions 2016

    @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


Log in to reply
 

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