Warning in Qt 5: "Using QCharRef with an index pointing outside the valid range of a QString"
-
Hi!
I have upgraded to Qt 5.15.2, using Qt Creator 4.14.2 and gcc 7.3.1 under Ubuntu Linux. I'm compiling my
project but I am seeing this annoying Warning everywhere:
"Using QCharRef with an index pointing outside the valid range of a QString. The corresponding behavior is deprecated, and will be changed in a future version of Qt."I want to supress this warning. Anyone can help me to know how to do it?
Thanks!
-
@AitorQt said in Warning in Qt 5: "Using QCharRef with an index pointing outside the valid range of a QString":
I want to supress this warning. Anyone can help me to know how to do it?
Fix your code.
-
@AitorQt said in Warning in Qt 5: "Using QCharRef with an index pointing outside the valid range of a QString":
There is no error
You're using an undocumented behavior which was removed in Qt6 so your app will crash with Qt6 and is using some undefined stuff in Qt5 so fix it.
If you don't like it then don't ask how to fix it. -
Hi
Just to be sure I understand.
so this auto-extend feature
https://doc.qt.io/qt-5/qstring.html#operator-5b-5d
was removed and hence the warning ? -
Now I understand @mrjj .
So, that means that using a so common operation like
QString str;
str[1] == ...
is deprecated? Then how is it supposed to be done? That has to do with the COW string to avoid expensive copies of strings, isn't it?Thanks for the answer
-
@AitorQt
I had no idea thatstr[1] == ...
orstr[1] = ...
beyond the string length was ever allowed, and would never have used it!For your
==
question, what is wrong withif (str.length() > 1 && str[1] == ...)
?For
=
, what about something likestr.resize(2, ' '); str[1] = ...;
? -
I have tested it.
Just adding the QString headers create the error. Doing just this.#include <QString>
#include <QStringList>int dummy = 1;
I run that and the warning appears 4 times! I suppose that the warning comes from the QStringList.h inside, probably the
library itself is doing it. -
@AitorQt said in Warning in Qt 5: "Using QCharRef with an index pointing outside the valid range of a QString":
That was the reason for the error
It was a warning, not an error.
Please mark the topic as solved then.