Solved Qt6 QString[] QChar compare with char
-
Hey
Say I have something like this...QString te("dcfsacsa") te[0] == "#"
What is the proper way of doing it under Qt6 as atm it just errors out with
error C2678: binary '==': no operator found which takes a left-hand operand of type 'QChar' (or there is no acceptable conversion)
-
@Dariusz said in Qt6 QString[] QChar compare with char:
te[0] == "#"
I know nothing about Qt6, or whether there is something special there. However, I think you have mistyped.
te[0]
isQChar
, but you are comparing against"#"
, which is a (literal) string, not a char. I think you intendedif (te[0] == '#') ...
-
That isn't a comparison between QString and char, but rather QChar and char. Convert the right argument to a QChar.
-
@Dariusz said in Qt6 QString[] QChar compare with char:
te[0] == "#"
I know nothing about Qt6, or whether there is something special there. However, I think you have mistyped.
te[0]
isQChar
, but you are comparing against"#"
, which is a (literal) string, not a char. I think you intendedif (te[0] == '#') ...
-
if you want to get singal character from your string
then you can useQString::at()
it return the character from your string at particular index.Try This
QString te("dcfsacsa"); if(te.at(0) == '#') { /// Your Code } else { /// Your Code }
-
Hey
Thanks! The QString::at() looks better than the QChar("#") I was thinking of doing. Should be faster I take?
Regards
Dariusz -
Ok so QString::at() returns QChar. So that is not help
Also I cant do QString[0] == QChar("#") because there is apparently no constructor for it?
I end up with QString[0] == QString("#")
I'm a bit worried with overhead here...
-
Also I cant do QString[0] == QChar("#") because there is apparently no constructor for it?
Did you read what I wrote above? I told you why your
"#"
won't work. It's a good idea to read and act on answers rather than ignoring them.... -
@JonB Oh buggers... I missed the " to ' change. Thanks! Yea that make more sense now o.o