qtsplitqt5qstringlist

QStringList split() function and simplifyWhiteSpace() function


I am using Qt5 and errors exist from this line of codes after running sample project I found on the internet.

QFile f( "world.txt" );

    if( f.open( QIODevice::ReadOnly ) )       {           
       QTextStream ts( &f );
       Vertex v[3];           
       int vcount = 0;             
       bool allok, ok;
      while( !ts.atEnd() )
        {

            QStringList line = QString::split( " ",ts.readLine().simplifyWhiteSpace() );

Errors are:

split is not a member of QStringList
simplifyWhiteSpace is not a member of QString

I don't know how to convert the line to work on Qt5.


Solution

  • Both QStringList::split() and QString::simplifyWhitespace() were functions in Qt3, and have been renamed or moved for Qt5 (which you are using according to your tags).

    For QStringList::split(), the documentation says:

    Use QString::split(sep, QString::SkipEmptyParts) or QString::split(sep, QString::KeepEmptyParts) instead.

    Be aware that the QString::split()'s return value is a QStringList that always contains at least one element, even if str is empty.

    You already changed this in your edit, so you are left with QString::simplifyWhitespace(), where the documentation says:

    QString QString::simplifyWhiteSpace () const

    Use simplified() instead.