Search code examples
c++qtqplaintextedit

Qt moveCursor no longer changes selection boundary


I am displaying text in a QPlainTextEdit, in a programming language (Basic) that can have a type-specific character at the end of an identifier; e.g. MyString$ or StartChar@. So if the user selects such an identifier by double-clicking, I want the type-specific character to be included in the selection. Here is my code to do that:

QChar last = document() -> characterAt (end - 1) ;
QChar next = document() -> characterAt (end) ;
if (isalnum (last.unicode())) switch (next.unicode())
  {
  case '@': case '%': case '!': case '&': case '$': case '^': case '#':
    moveCursor (QTextCursor::Right, QTextCursor::KeepAnchor) ;
    break ;
  }

This used to work fine. But now I have upgraded to Qt 5.12 (from Qt 5.4, I think), the call to moveCursor no longer causes the selection to include the extra character. Does anybody have an idea why this behaviour has changed, and what I can do about it?


Solution

  • The posted code works as expected. What seems to have changed is that after a double click, QPlainTextEdit triggers a mouseReleaseEvent; it was this event that was not being handled correctly in my code.

    I have fixed this, and now everything works as it used to.