Search code examples
c++qtselectiontextselectionqdialog

Make a QDialog read only, but still with text selectable


Disclaimer: I'm not a Qt programmer, I'm just asking in case someone could solve an issue about DB Browser for SQLite.

So, an EditDialog widget is extending the QDialog class. As described in the issue, in some scenarios this widget is readonly, but it would be great if its text content could still be selectable.

There are two use cases:

  • text selection with the mouse
  • blinking caret, text selection with keyboard Shift + arrows

The current solution fixes only the first case. There is no blinking caret and keyboard selection doesn't work. Could this be improved?


Solution

  • Assuming the textarea is a QTextEdit or QPlainTextEdit, you should be able to fix this by adjusting the text interaction flags. Try something like this:

    ui->editorText->setTextInteractionFlags(
        Qt.TextSelectableByMouse | Qt.TextSelectableByKeyboard)