Code doesnt work



  • Hellow, plaease, tell me why my code doesnt work?
    @bool isClass = false;
    for(int a=0;a<=needNext.count();a++)
    {
    if (QString(needNext[a])!=QString(" "))
    if (QString(needNext[a])==QString("}"))
    {
    qDebug() <<"1"+ QString(needNext[a]);
    isClass=true;
    a=needNext.count();
    }
    else
    {
    a=needNext.count();
    qDebug() <<"2"+ QString(needNext[a]);
    }
    }@
    It is must find last symbol in text unequal " "(space) and if last symbol equal "{" should happen assignment
    It is always display "2 (without second qoute)
    As the text is supplied text
    @QPushButton{
    background-color: #123123;
    }@
    for example



  • do you have a new line after your " ", It may be better to use regex to find your last space
    [Edit]
    A good way to debug this may be to print which character you are seeing last you may want to print this as a int value



  • I dont have new line after " "



  • If the text = "}" code worke right, but if text = "a}" code doesnt work wright



  • What Type is needNext?
    I suspect that you would iterate over that loop 1 to many times it should probably be a< needNext.count(); as count() probably returns the size and you probably require the size -1



  • There is an ambiguous 'else' here. put contents of every if in {} always. also you don't need to convert QChar s to QString . they are themselves comparable.
    I assume needNext is a QString (is not?)
    try tis code:

    @ for(int a=0; a<=needNext.count(); a++)
    {
    if (needNext[a]!=QChar(' '))
    {
    if (needNext[a]==QChar('}'))
    {
    qDebug() <<"1"+ needNext[a];
    isClass=true;
    // a=needNext.count();
    // probably you mean:
    break;
    }
    else
    {
    //a=needNext.count();
    break;
    qDebug() <<"2"+ needNext[a];
    }
    }
    }@
    If you want to iterate over all characters of a QString, you may want to use a bit more elegant way using iterators:
    @
    QString::ConstIterator i = needNext.begin();
    while(i != needNext.end())
    {
    if ( *i !=QChar(' '))
    {
    if ( *i ==QChar('}'))
    {
    // ...
    i++;
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.