ASSERT occurs on a.at(5).isNull()



  • Here is the code I'm trying:
    #include <QTextStream>

    int main(void)
    {

    QTextStream out(stdout);
    
    QString a = "Eagle";
    
    out << a[0] << endl;
    out << a[4] << endl;
    
    out << a.at(0) << endl;
    
    if (a.at(5).isNull())
    {
        out << "Outside the range of the string" << endl;
    }
    
    return 0;
    

    }
    Here is the output:
    E
    e
    E
    ASSERT: "uint(i) < uint(size())" in file ../../Qt/5.6/clang_64/lib/QtCore.framework/Headers/qstring.h, line 868

    Isn't a.at(5).isNull() the correct coding?



  • The assertion is expected behaviour for QString::at

    The position must be a valid index position in the string (i.e., 0 <= position < size()).

    Instead, you should do something like:

    if (5 >= a.size())
    {
        out << "Outside the range of the string" << endl;
    }
    

    Cheers.


Log in to reply
 

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