Important: Please read the Qt Code of Conduct -

Comparison of QChars

  • Hi guys,
    sry for this nooby question, but i am trying to compare a QChar out of a QString with a simple char and i dont get it.

    I have simply a line of numbers and want to seperate it where a ":" is.
    For example 315:15 -> x=315 , y=15

    here is my code

    QString part1="";
    QString part2="";
    bool before;
    for(int y=0; y < line.size(); y++){
            if( before &&!=":")){
                            part1 +=;
             }else if(":"){
                            before = false;
             }else if(!before &&""){
                            part2 +=;

    In this case i get the errror:

     error: no match for 'operator==' (operand types are 'const QChar' and 'const char [2]')

    so i tried to cast the char, but it is still a QChar[2] . And i do not understand how a simple char can be a QChar array of 2.
    So i tried:

    QChar seperator = (QChar)(":");

    Same problem. So what i am doing wrong here?

  • Moderators

    I think, the error in your code is here:

    }else if(!before &&""){
                            part2 +=;

    thats a typo, = needs to be a == for a valid if statement.

    Anyway have you thought about useing


    could make things simpler for you.

  • oh ,
    in the code i had i correctly.
    But i always tells me that ":" is an const char [2]

    But split() is exactly what i was looking for.
    Thanks a lot for that.

  • Side Note: the problem your compiler is warning you about is that ":" is actually a string so 2 chars, a : and the null termination(\0). use single quotes for single chars, ':'

  • Moderators

    If I may - split() is horrible. It creates a list of copies - really bad for performance. Please use splitRef() when parsing data. It creates a vector of StringRefs, a much better solution if you don't need to copy the data (and you don't need to here) and a much more cache friendy structure.

Log in to reply