0

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?

TonyK
  • 16,761
  • 4
  • 37
  • 72
  • According to the doc [https://doc.qt.io/qt-5/qplaintextedit.html#moveCursor](https://doc.qt.io/qt-5/qplaintextedit.html#moveCursor) 5.12 should behave exactly as 5.4. What does happen exactly? Does the cursor move but the selection stay the same? Does the selection change? – Sergio Monteleone Mar 30 '19 at 12:01
  • @SergioMonteleone: As far as I can tell, the call to `moveCursor` has no effect at all. The cursor stays at the end of the selection, before the special character. – TonyK Mar 30 '19 at 12:02
  • Can you check if the ```moveCursor``` is called, placing a breakpoint there? – Sergio Monteleone Mar 30 '19 at 13:17
  • @SergioMonteleone: Yes, I have stepped through it, and `moveCursor` is definitely called. – TonyK Mar 30 '19 at 13:23
  • @SergioMonteleone: I have found the problem -- see my answer. – TonyK Apr 03 '19 at 14:15

1 Answers1

0

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.

TonyK
  • 16,761
  • 4
  • 37
  • 72
  • I'm glad you fixed the issue but I think it was just a side effect in your code. According to the archived docs (event for the old Qt 4) a double click will **always** fire two press and two release events. Please have a look at the documentation here: (https://doc.qt.io/archives/qt-5.5/qwidget.html)[https://doc.qt.io/archives/qt-5.5/qwidget.html) – Sergio Monteleone Apr 03 '19 at 16:50
  • @SergioMonteleone: Then perhaps it's down to the exact order of the various events: mouseDown - moveCursor - mouseRelease versus mouseDown - mouseRelease - moveCursor. It is easy to imagine such a thing changing between Qt versions. – TonyK Apr 03 '19 at 17:01