admin管理员组

文章数量:1401673

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?

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?

Share Improve this question edited Mar 25 at 2:26 musicamante 48.7k8 gold badges41 silver badges74 bronze badges asked Mar 24 at 13:10 CryptkeeperCryptkeeper 1631 silver badge9 bronze badges 1
  • 1 Setting the readOnly property is fundamentally the same as calling setTextInteractionFlags() with related options, and toggling a checkbox actually changes the contents of the document, therefore you cannot make the document read only and still keep checkboxes interactive. The only way to achieve this is by overriding mouse handlers (press, move and release), get the document's documentLayout() and call blockWithMarkerAt(), then eventually work with it based on the mouse event, if the block is valid. – musicamante Commented Mar 24 at 20:55
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: cUse QTextEdit or QTextBrowser readonly but keep MarkDown Checkboxes clickableStack Overflow