vkbo / novelWriter

novelWriter is an open source plain text editor designed for writing novels. It supports a minimal markdown-like syntax for formatting text. It is written with Python 3 (3.9+) and Qt 5 (5.15) for cross-platform support.
https://novelwriter.io
GNU General Public License v3.0
2.01k stars 102 forks source link

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

Open tmarplatt opened 2 weeks ago

tmarplatt commented 2 weeks ago

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.

vkbo commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 1 week ago

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 commented 1 week ago

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.