Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QTextCursor::setPosition: Position 'XX' out of range #2010

Open
tmarplatt opened this issue Aug 30, 2024 · 7 comments
Open

QTextCursor::setPosition: Position 'XX' out of range #2010

tmarplatt opened this issue Aug 30, 2024 · 7 comments
Labels
bug Issue: Something isn't working investigate Meta: Requires further investigation

Comments

@tmarplatt
Copy link
Contributor

For any new project created with v2.5.1, I'm seeing this error in the console continuously printed as I type in the Editor:

...
QTextCursor::setPosition: Position '34' out of range
QTextCursor::setPosition: Position '35' out of range
QTextCursor::setPosition: Position '36' out of range
QTextCursor::setPosition: Position '37' out of range
QTextCursor::setPosition: Position '38' out of range
...

I've been able to reproduce this running from source (using Qt version 5.15.3, Python 3.10.12) and from the AppImage file. With a fresh $HOME/.config/novelwriter, it's not as easy to reproduce:

  1. start a new project
  2. type some text
  3. click out of the novelwriter window
  4. click back in, preferrably below the document text in the Editor
  5. type again

One symptom of this seems to be that novelWriter fails to highlight quoted text.

@tmarplatt tmarplatt added the bug Issue: Something isn't working label Aug 30, 2024
@vkbo vkbo added the investigate Meta: Requires further investigation label Aug 30, 2024
@vkbo
Copy link
Owner

vkbo commented Aug 30, 2024

I cannot reproduce this from your description. It could be related to some settings, so if you could post your config file? You can delete settings that point to local paths on your computer.

The error itself is probably harmless. Qt sometimes prints errors, and many of them are beyond my control. I'm not sure it is related to highlighting. It is probably related to one of the format/auto-format features instead, which is where the setPosition call is made a lot.

@tmarplatt
Copy link
Contributor Author

Here's my novelwriter.conf file:

[Meta]
timestamp = 2024-09-01 01:37:29

[Main]
font = Ubuntu,10,-1,5,50,0,0,0,0,0
theme = default
syntax = default_light
localisation = en_GB
hidevscroll = False
hidehscroll = False
lastnotes = 0x020501f0
nativefont = True

[Sizes]
mainwindow = 1200, 650
welcome = 800, 550
preferences = 700, 615
mainpane = 300, 860
viewpane = 500, 150
outlinepane = 13, 13

[Project]
autosaveproject = 60
autosavedoc = 30
emphlabels = True
backuppath = /home/tmarplatt/Backups
backuponclose = False
askbeforebackup = True

[Editor]
textfont = Ubuntu,10,-1,5,50,0,0,0,0,0
width = 700
margin = 40
tabwidth = 40
focuswidth = 800
hidefocusfooter = False
justify = False
autoselect = True
autoreplace = True
repsquotes = True
repdquotes = True
repdash = True
repdots = True
autoscroll = False
autoscrollpos = 30
scrollpastend = True
fmtsquoteopen = ‘
fmtsquoteclose = ’
fmtdquoteopen = “
fmtdquoteclose = ”
fmtpadbefore = 
fmtpadafter = 
fmtpadthin = False
spellcheck = en
showtabsnspaces = False
showlineendings = False
showmultispaces = True
incnoteswcount = True
showfullpath = True
dialogstyle = 2
allowopendial = True
narratorbreak = 
altdialogopen = 
altdialogclose = 
dialogline = 
highlightemph = True
stopwhenidle = True
useridletime = 300

[State]
showviewerpanel = False
showedittoolbar = False
viewcomments = True
viewsynopsis = True
searchcase = False
searchword = False
searchregex = False
searchloop = False
searchnextfile = False
searchmatchcap = False
searchprojcase = False
searchprojword = False
searchprojregex = False

As created by the AppImage launcher, with reproducible behavior.

@vkbo
Copy link
Owner

vkbo commented Sep 1, 2024

I still can't reproduce it. Neither from AppImage nor source, using your config and following your steps precisely.

I do have a different Qt version when running from source, 5.15.8, but in the AppImage it is bundled and should be the same.

What remains is to test with the same OS as you. Are you on Ubuntu or Mint?

@vkbo
Copy link
Owner

vkbo commented Sep 1, 2024

I found two Qt bugs that may or may not be related. They too cannot be reproduced, but there could be something related to how Qt handles input methods. See QTBUG-98948 and QTBUG-94720.

I cannot find any issue in any use of setPosition in the novelWriter code. That would also likely be a lot easier to reproduce. I can trigger the same error message by deliberately setting out of range positions, so I can confirm that a miscalculation of position in novelWriter will trigger the same warning. But setPosition is also called very often by the Qt library itself.

The warning comes from qtextcursor.cpp#L1129. It checks against its internally recorded length before issuing the warning, and there seem to be some bugs related to how the length is handled for some input methods, like virtual keyboards. I have no idea if that's related, but at least one issue was fixed for 5.15.4, although this one showed up on Android.

Until I can reproduce it, I simply don't have any way to tell where it comes from; novelWriter or Qt,

@tmarplatt
Copy link
Contributor Author

tmarplatt commented Sep 3, 2024

I'm using Linux Mint 22 Cinnamon. With the fresh config file, I can reproduce this simply by clicking below the bottom-most line in the Editor, right after creating a new project.

@vkbo
Copy link
Owner

vkbo commented Sep 3, 2024

Does it have to be a new project, and if so, which file is open in the editor?

I tried on Mint 21.3 (same Qt version) and at least there I could not reproduce it either.

Simply clicking in the editor below the last character only triggers the update of the cursor position in the document footer, but there is no call to setPosition made there (it only reads the position) so I'm starting to think this must be internal in the Qt library somewhere.

I'm not sure if it is a quirk of the specific versions, the system locale (the document length calculation could be off for some non-ascii letters), or something different.

In any event, the message itself is harmless, as I said. The call to setPosition is simply cancelled after the message is printed. I have no way of tracking it down without reproducing it, so I'm stuck at this point.

@vkbo
Copy link
Owner

vkbo commented Oct 13, 2024

One symptom of this seems to be that novelWriter fails to highlight quoted text.

This part made me a little curious. I have recently discovered that the QRegularExpression, when wrapped by PyQt, sometime returns garbage matches. I discovered it when testing upgrading novelWriter to Qt 6.4. It produced a number of Unicode errors in the spell checker. The highlighter uses the same regex object as the spell checker.

I only suspect the issue is related to Unicode, but I cannot be certain. Python uses fixed 1, 2 or 4 byte encoding depending on the string, while Qt uses UTF 16. The reason I suspect it is that only some strings show the problem, and I bet it's related to whatever encoding Python ends up using.

It's possible this is an issue isolated to QRegularExpression in Qt 6.4, or it may be some underlying weakness that shows up other places too. In any case, for the novelWriter 2.6 branch, I've replaced all use of QRegularExpression with the internal Python regex engine. There is even a minor performance improvement as well.

I haven't checked the Qt source, but I would expect the syntax highlighter on the Qt side also calls setPosition, and it could be the cause here. Especially since you see an effect on the highlighter too.

Would you be able to run the current main branch and see if the issue persists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue: Something isn't working investigate Meta: Requires further investigation
Projects
None yet
Development

No branches or pull requests

2 participants