Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

`QTextList` inside `QTextEdit` not working as expected



  • Scenario:

    In my QTextEdit I've created a QTextList. Calling toHtml(), the output looks like this (formatted with a tool, style and html 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 on Line two, and createList(...) is called.
    The expected output is a new ol in the existing ol (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 with PreviousBlock, to move inside the newly created block. Then call createList(...)

    <body>
        <ol>
            <li>Line one</li>
            <ol>
                <li>
                    <br />
                </li>
            </ol>
            <li>Line two</li>
        </ol>
    </body>
    

    Excellent. Fantastic. The ol is inside the other ol now!
    Now, move back to the original block we were at with NextBlock
    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 call insertHtml(...) with a properly(?) formatted, nested ol.

    The actual question:

    Is this a Qt bug? Is this a limitation with the fact QTextEdit uses HTML4? Is there an alternative that I can use that works as expected? Any help would be really appreciated.


  • Lifetime Qt Champion

    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 of Line two and press F8/F9 to produce the same HTML I've described above.


  • Lifetime Qt Champion

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


Log in to reply