Warning in Qt 5: "Using QCharRef with an index pointing outside the valid range of a QString"
-
wrote on 28 Dec 2021, 16:01 last edited by
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!
-
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.
-
wrote on 28 Dec 2021, 16:25 last edited by
There is no error, just an unending list of warning which avoid me focusing in the real errors and warnings.
-
Lifetime Qt Championwrote on 28 Dec 2021, 16:26 last edited by Christian Ehrlicher
@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 ? -
wrote on 28 Dec 2021, 23:53 last edited by
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
-
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
wrote on 29 Dec 2021, 07:30 last edited by JonB@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] = ...;
? -
wrote on 29 Dec 2021, 10:57 last edited by
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. -
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. -
wrote on 29 Dec 2021, 11:45 last edited by
Ok, I have found it.
The error was trying to access an empty QString.
QString str = "";
str[0] == ...
that was the reason for the error -
Ok, I have found it.
The error was trying to access an empty QString.
QString str = "";
str[0] == ...
that was the reason for the error@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.
-
wrote on 29 Dec 2021, 12:59 last edited by
Yeah, done!
1/12