Unsolved `QTextList` inside `QTextEdit` not working as expected
-
Scenario:
In my
QTextEdit
I've created aQTextList
. CallingtoHtml()
, the output looks like this (formatted with a tool,style
andhtml
removed from all code blocks to reduce noise)<body> <ol> <li>Line one</li> <li>Line two</li> </ol> </body>
The cursor is moved to before the
L
onLine two
, andcreateList(...)
is called.
The expected output is a newol
in the existingol
(I've also tried changing all attributes of the format object that the API exposed, with no effect)<body> <ol> <li>Line one</li> </ol> <ol> <li>Line two</li> </ol> </body>
Not quite what I wanted.
I also tried...
...doing it the very-long-winded way.
Start with the same list:
<body> <ol> <li>Line one</li> <li>Line two</li> </ol> </body>
first call
insertBlock()
to create a new "dummy" block<body> <ol> <li>Line one</li> <li> <br /> </li> <li>Line two</li> </ol> </body>
call
cursor.movePosition
withPreviousBlock
, to move inside the newly created block. Then callcreateList(...)
<body> <ol> <li>Line one</li> <ol> <li> <br /> </li> </ol> <li>Line two</li> </ol> </body>
Excellent. Fantastic. The
ol
is inside the otherol
now!
Now, move back to the original block we were at withNextBlock
Inside the block (Line two
),add(...)
the current block (cursor.block()
) to the newly created list:<body> <ol> <li>Line one</li> </ol> <ol> <li> <br /> </li> <li>Line two</li> </ol> </body>
Huh? It's suddently jumped outside of the original
ol
again.The same behaviour of the last
ol
jumping outside also happened when trying to callinsertHtml(...)
with a properly(?) formatted, nestedol
.The actual question:
Is this a Qt bug? Is this a limitation with the fact
QTextEdit
usesHTML4
? Is there an alternative that I can use that works as expected? Any help would be really appreciated. -
Hi and welcome to devnet,
What version of Qt are you using ?
Can you provide the code you are using to generate that html ? -
What version of Qt are you using ?
5.14.1
Can you provide the code you are using to generate that html ?
#include <QApplication> #include <QTextListFormat> #include <QTextEdit> #include <QKeyEvent> #include <QtGlobal> #include <QTextList> #include <QDebug> class Editor : public QTextEdit { public: Editor() { auto cursor = textCursor(); QTextListFormat fmt; fmt.setStyle(QTextListFormat::ListDecimal); cursor.createList(fmt); cursor.insertText("Line one\nLine two"); }; protected: void keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_F8) { auto cursor = textCursor(); auto list = cursor.currentList(); auto fmt = list->format(); fmt.setIndent(fmt.indent() + 1); cursor.createList(fmt); qDebug().noquote() << toHtml(); } else if (event->key() == Qt::Key_F9) { auto cursor = textCursor(); cursor.insertBlock(); cursor.movePosition(QTextCursor::PreviousBlock); auto list = cursor.currentList(); auto fmt = list->format(); fmt.setIndent(fmt.indent() + 1); auto new_list = cursor.createList(fmt); qDebug().noquote() << toHtml(); cursor.movePosition(QTextCursor::NextBlock); new_list->add(cursor.block()); qDebug().noquote() << toHtml(); } else { QTextEdit::keyPressEvent(event); } } }; int main(int argc, char* argv[]) { QApplication app(argc, argv); Editor editor; editor.show(); return app.exec(); }
Here's a program where I've recreated everything I described above.
Move the cursor before the
L
ofLine two
and press F8/F9 to produce the same HTML I've described above. -
I'd say you likely have found a bug.
I'd recommend taking a look at the bug report system to see if there's something related to it there. If not, please open a new report providing your example project there (please include a .pro file so it can be directly compiled).