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

QTextStream to fill a QString



  • Hi all - I am afraid I have a big missconception here.

    I tried to format a QString by piping stuff in via QTextStream. However, after some hundret characters everything gets messed up and the resulting QString ends in unreadable characters. The code looks like this:

        QString all;
        QTextStream sout(&all);
        sout << Qt::endl;
        sout << qsl("Some String :") << Qt::endl << someObject[0].toString() << Qt::endl;
        sout << qsl("Some String ") << Qt::endl << someObject[1].toString() << Qt::endl;
        sout << qsl("Some String ") << Qt::endl << someObject[2].toString() << Qt::endl;
    
        sout << qsl("Some String :") << Qt::endl << someObject[0].toString() << Qt::endl;
        sout << qsl("Some String :") << Qt::endl << someObject[1].toString() << Qt::endl;
        sout << qsl("Some String :") << Qt::endl << someObject[2].toString() << Qt::endl;
    (some more)
    
        sout.flush();
        return all;
    

    Is this not a valid use of QTextStream? Is the QString not auto-resizing? ... Who can give me a hint?

    regards
    Holger



  • Oh, wow - this is not about QTextStream at all - sorry for misleading. I can recreate the effect by just writing a long QString to qDebug()!
    And this was reported before: https://forum.qt.io/topic/72244/limitation-of-string-length-for-qdebug-output/7

    Best regards
    Holger

    PS: this was enough to reproduce:

    #include "mainwindow.h"
    
    #include <QDebug>
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        qDebug() <<
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------";
    
        return a.exec();
    }
    
    

  • Lifetime Qt Champion

    Hi
    What does someObject[0].toString() return ?
    Do you have any std:: strings you call c_str() on or anything else that could become invalid later?



  • the .toString() functions return QStrings. Actually it is the output of some .arg(...) call:

        QString out =
              "#nbr :%1"
            "; #nbr      :%2"
            "; float   (e):%3";
    
        return out.arg(QString::number(nbrCreditors), QString::number(nbrContracts), QString::number(value, 'f', 2));
    
    

  • Lifetime Qt Champion

    @HoMa
    Hmm. I cant really make it do odd things.
    Can you try

       QString all;
        QTextStream sout(&all);
        for (int c=0; c < 1000; c++ )
         sout << "this is a test" << Qt::endl;
    


  • I initialized the QString like this

    QString all(2048, '\0');
    

    and the problem does not occure ...
    Strange world. However: This should work, right?

    Holger


  • Lifetime Qt Champion

    @HoMa
    Hi
    Yes it should work as i just tried in a small test app and it
    show the 1000 lines fine in a PlainTextEdit.
    (Qt 5.15, 64 bit)
    What Qt version do you use ? and 32 or 64 bit ?

    this worked fine.. ?

     QString all;
        QTextStream sout(&all);
        for (int c=0; c < 1000; c++ )
         sout << "this is a test" << Qt::endl;
    
        ui->plainTextEdit->appendPlainText(all);
    


  • Thx.

    I am on Qt Creator 4.13.0, just currently updated. This was the first update since the fresh install (I ... think).

    Best Regards
    Holger



  • Oh, wow - this is not about QTextStream at all - sorry for misleading. I can recreate the effect by just writing a long QString to qDebug()!
    And this was reported before: https://forum.qt.io/topic/72244/limitation-of-string-length-for-qdebug-output/7

    Best regards
    Holger

    PS: this was enough to reproduce:

    #include "mainwindow.h"
    
    #include <QDebug>
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        qDebug() <<
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------"
            "----------------------------------------------------------------------------------------------------";
    
        return a.exec();
    }
    
    

  • Lifetime Qt Champion

    Just as follow up: this seems to be a limitation of GDB on Windows.

    See QTCREATORBUG-24649 for the continuation discussion.

    Regards


Log in to reply