c++qtqtexteditqtextbrowser

Use QTextEdit or QTextBrowser readonly but keep MarkDown Checkboxes clickable


I want to display MarkDown in a QTextEdit or a QTextBrowser as read-only. However, I want to be able to toggle MarkDown checkboxes. But if I set setReadOnly(true); the checkboxes are not clickable.

I tried various combinations of setTextInteractionFlags(Qt::LinksAccessibleByMouse); etc. but either the whole text is editable or the checkboxes are not clickable.

Is there a way to achieve this?


Solution

  • I found a more or less "hacky" workaround by overriding mouse handlers but without the need to handle the document manipulation itself, by enabling write "just in time":

    void ReadOnlyTextEdit::mouseReleaseEvent(QMouseEvent *event)
    {
        setReadOnly(false);
        QTextEdit::mouseReleaseEvent(event);
        setReadOnly(true);
    }
    
    void ReadOnlyTextEdit::mouseMoveEvent(QMouseEvent *event)
    {
        setReadOnly(false);
        QTextEdit::mouseMoveEvent(event);
        setReadOnly(true);
    }
    

    So far it seems to work and I have not found any drawbacks yet.