Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextStream to fill a QString

QTextStream to fill a QString

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HoMa
    wrote on 15 Sept 2020, 16:46 last edited by
    #1

    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

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HoMa
      wrote on 15 Sept 2020, 19:23 last edited by HoMa
      #8

      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();
      }
      
      
      A 1 Reply Last reply 16 Sept 2020, 18:13
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 15 Sept 2020, 16:54 last edited by
        #2

        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?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HoMa
          wrote on 15 Sept 2020, 16:59 last edited by
          #3

          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));
          
          
          M 1 Reply Last reply 15 Sept 2020, 17:08
          0
          • H HoMa
            15 Sept 2020, 16:59

            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));
            
            
            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 15 Sept 2020, 17:08 last edited by
            #4

            @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;
            
            1 Reply Last reply
            0
            • H Offline
              H Offline
              HoMa
              wrote on 15 Sept 2020, 17:12 last edited by
              #5

              I initialized the QString like this

              QString all(2048, '\0');
              

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

              Holger

              M 1 Reply Last reply 15 Sept 2020, 17:18
              0
              • H HoMa
                15 Sept 2020, 17:12

                I initialized the QString like this

                QString all(2048, '\0');
                

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

                Holger

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 15 Sept 2020, 17:18 last edited by mrjj
                #6

                @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);
                
                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HoMa
                  wrote on 15 Sept 2020, 18:33 last edited by
                  #7

                  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

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    HoMa
                    wrote on 15 Sept 2020, 19:23 last edited by HoMa
                    #8

                    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();
                    }
                    
                    
                    A 1 Reply Last reply 16 Sept 2020, 18:13
                    1
                    • H HoMa
                      15 Sept 2020, 19:23

                      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();
                      }
                      
                      
                      A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 16 Sept 2020, 18:13 last edited by
                      #9

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

                      See QTCREATORBUG-24649 for the continuation discussion.

                      Regards

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      3

                      2/9

                      15 Sept 2020, 16:54

                      topic:navigator.unread, 7
                      • Login

                      • Login or register to search.
                      2 out of 9
                      • First post
                        2/9
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved