Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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] is QChar, but you are comparing against "#", which is a (literal) string, not a char. I think you intended

    if (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] is QChar, but you are comparing against "#", which is a (literal) string, not a char. I think you intended

    if (te[0] == '#') ...
    


  • if you want to get singal character from your string
    then you can use QString::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...



  • @Dariusz

    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


Log in to reply