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

How to use QTextListFormat?



  • Hi!

    I am currently experimenting with the Qt Quick Text Editor and wanted to extend the example with a list function. But I do not understand the documentation and could not find an example on how QTextListFormat is used. Here is what I did so far:

    // added to .h 
         Q_PROPERTY(bool ul READ ul WRITE setUl NOTIFY ulChanged)
    public:
        bool ul() const;
        void setUl(bool ul);
    
    // added to .cpp
    bool DocumentHandler::ul() const
    {
        QTextCursor cursor = textCursor();
        if (cursor.isNull())
            return false;
        qDebug()<< "is list?" << cursor.blockFormat().isListFormat();
        return cursor.blockFormat().isListFormat();
    }
    
    void DocumentHandler::setUl(bool ul)
    {
        qDebug() << "ul is: " << ul;
        QTextCursor cursor = textCursor();
        QTextListFormat list;
    
        if(ul){
            list.setStyle(QTextListFormat::ListDisc);
        }else{
            list.clearProperty(QTextListFormat::ListDisc);
        }
    
        cursor.insertList(list);
    
        emit ulChanged();
    }
    

    isListFormat always returns false. I guess I am not using it correctly. How can I check if the cursor is positioned in a list?

    setUl works for setting a list. But the list is always started in a new line and in case the cursor has a selection the selected text is deleted. Also I guess unsetting the list is done wrong as well but there is nothing in the docs on how to remove a list format from a text block.

    Could someone point me to some explanation?


Log in to reply