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!
-
@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.htmlas 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...
-
@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... -
@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.
-
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...
-
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"); -
@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...
-
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
-
@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