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 question
Forum Updated to NodeBB v4.3 + New Features

QTextStream question

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 1.6k 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.
  • Q qAlexKo

    @JonB said in QTextStream question:

    . Could you show just what output you do get with your code?

    010.020

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #4

    @qAlexKo
    Your reply crossed with my update. At least one of those 0s is (unfortunately) for the . field. Start by getting rid of that to see what is going on. And another 0 may be from the << Qt::endl? I think you have those for each <<. You are getting something like (with spacing so we can read):

    01  0.  02  0\n
    
    Q 1 Reply Last reply
    1
    • JonBJ JonB

      @qAlexKo
      Your reply crossed with my update. At least one of those 0s is (unfortunately) for the . field. Start by getting rid of that to see what is going on. And another 0 may be from the << Qt::endl? I think you have those for each <<. You are getting something like (with spacing so we can read):

      01  0.  02  0\n
      
      Q Offline
      Q Offline
      qAlexKo
      wrote on last edited by
      #5

      @JonB said in QTextStream question:

      At least one of those 0s is (unfortunately) for the . field

          int a=1;
          cout << qSetPadChar('0') << qSetFieldWidth(2)  << a << Qt::endl;
      

      Output: 010

      JonBJ 2 Replies Last reply
      0
      • Q qAlexKo

        @JonB said in QTextStream question:

        At least one of those 0s is (unfortunately) for the . field

            int a=1;
            cout << qSetPadChar('0') << qSetFieldWidth(2)  << a << Qt::endl;
        

        Output: 010

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #6

        @qAlexKo Like I said, remove << Qt::endl?

        Q 1 Reply Last reply
        1
        • Q qAlexKo

          @JonB said in QTextStream question:

          At least one of those 0s is (unfortunately) for the . field

              int a=1;
              cout << qSetPadChar('0') << qSetFieldWidth(2)  << a << Qt::endl;
          

          Output: 010

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #7

          @qAlexKo
          If/when this does not get you quite what you want, or it's too difficult/fiddly/messy. Since you only want those options against (single digit) integers, but they will like affect everything on the stream, I would use QString::number() or other of QString numeric formatting methods to produce desired number format and send that as a string rather then fiddling with QTextStream formatting directives.

          1 Reply Last reply
          0
          • JonBJ JonB

            @qAlexKo Like I said, remove << Qt::endl?

            Q Offline
            Q Offline
            qAlexKo
            wrote on last edited by qAlexKo
            #8

            @JonB said in QTextStream question:

            @qAlexKo Like I said, remove << Qt::endl?

            Yes, the last cout << Qt:endl is to blame.

                int a=1;
                cout << "test" << Qt::endl;
                cout << qSetPadChar('0') << qSetFieldWidth(2) << a;
                //cout << Qt::endl;
            

            If I stream to QTextStream everything is OK

                QString qstr;
                QTextStream qstream(&qstr);
                int aa=1;
                qstream << qSetPadChar('0') << qSetFieldWidth(2) << aa;
                cout << qstr << Qt::endl;
            
            JonBJ 1 Reply Last reply
            0
            • Q qAlexKo

              @JonB said in QTextStream question:

              @qAlexKo Like I said, remove << Qt::endl?

              Yes, the last cout << Qt:endl is to blame.

                  int a=1;
                  cout << "test" << Qt::endl;
                  cout << qSetPadChar('0') << qSetFieldWidth(2) << a;
                  //cout << Qt::endl;
              

              If I stream to QTextStream everything is OK

                  QString qstr;
                  QTextStream qstream(&qstr);
                  int aa=1;
                  qstream << qSetPadChar('0') << qSetFieldWidth(2) << aa;
                  cout << qstr << Qt::endl;
              
              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #9

              @qAlexKo
              Yes, I thought so. See my latest message: for simplicity I suggest you do your formatting work to produce your desired string and then send that to the text stream. Looks simpler.

              Q 1 Reply Last reply
              0
              • Q qAlexKo has marked this topic as solved on
              • JonBJ JonB

                @qAlexKo
                Yes, I thought so. See my latest message: for simplicity I suggest you do your formatting work to produce your desired string and then send that to the text stream. Looks simpler.

                Q Offline
                Q Offline
                qAlexKo
                wrote on last edited by qAlexKo
                #10

                @JonB said in QTextStream question:

                Looks simpler

                In the full variant "01.02" it is important to set zero width for "." output.

                    QString qstr;
                    QTextStream qstream(&qstr);
                    int aa=1, bb=2;
                    qstream << qSetPadChar('0') << qSetFieldWidth(2) << aa << qSetFieldWidth(0) << "." << qSetFieldWidth(2) << bb;
                    cout << qstr << Qt::endl;
                
                
                JonBJ 1 Reply Last reply
                0
                • Q qAlexKo

                  @JonB said in QTextStream question:

                  Looks simpler

                  In the full variant "01.02" it is important to set zero width for "." output.

                      QString qstr;
                      QTextStream qstream(&qstr);
                      int aa=1, bb=2;
                      qstream << qSetPadChar('0') << qSetFieldWidth(2) << aa << qSetFieldWidth(0) << "." << qSetFieldWidth(2) << bb;
                      cout << qstr << Qt::endl;
                  
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #11

                  @qAlexKo said in QTextStream question:

                  In the full variant "01.02" it is important to set zero width for "." output.

                  So? I would build a string for the whole of 01.02, or for each number separately with a "." field and set the "gap" between fields to zero/empty. Up to you.

                  Q 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @qAlexKo said in QTextStream question:

                    In the full variant "01.02" it is important to set zero width for "." output.

                    So? I would build a string for the whole of 01.02, or for each number separately with a "." field and set the "gap" between fields to zero/empty. Up to you.

                    Q Offline
                    Q Offline
                    qAlexKo
                    wrote on last edited by qAlexKo
                    #12

                    @JonB

                    The variant with cout also works if do this:

                        QTextStream cout(stdout);
                        int a=1, b=2;
                        cout << qSetPadChar('0') << qSetFieldWidth(2) << a << qSetFieldWidth(0) << "." << qSetFieldWidth(2) << b << qSetPadChar(0) << Qt::endl;
                    
                    
                    JoeCFDJ JonBJ 2 Replies Last reply
                    0
                    • Q qAlexKo

                      @JonB

                      The variant with cout also works if do this:

                          QTextStream cout(stdout);
                          int a=1, b=2;
                          cout << qSetPadChar('0') << qSetFieldWidth(2) << a << qSetFieldWidth(0) << "." << qSetFieldWidth(2) << b << qSetPadChar(0) << Qt::endl;
                      
                      
                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #13

                      @qAlexKo you can create one QString with arg.

                          int a = 1;
                          int b = 2;
                      
                          std::cout << qPrintable( QString( "%1." ).arg( a, 2, 10, QChar( '0' ) ) ) 
                                    << qPrintable( QString( "%1" ).arg( b, 2, 10, QChar( '0' ) ) ) << std::endl;
                      
                      Q 1 Reply Last reply
                      1
                      • Q qAlexKo

                        @JonB

                        The variant with cout also works if do this:

                            QTextStream cout(stdout);
                            int a=1, b=2;
                            cout << qSetPadChar('0') << qSetFieldWidth(2) << a << qSetFieldWidth(0) << "." << qSetFieldWidth(2) << b << qSetPadChar(0) << Qt::endl;
                        
                        
                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #14

                        @qAlexKo said in QTextStream question:

                        The variant with cout also works if do this:

                        Yes, which is why I wrote earlier that persisting to do it this way gets "difficult/fiddly/messy" and that using a QString method like number() or arg() seems so much easier. I can't keep repeating the same thing so I have said my piece now.

                        JoeCFDJ 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @qAlexKo said in QTextStream question:

                          The variant with cout also works if do this:

                          Yes, which is why I wrote earlier that persisting to do it this way gets "difficult/fiddly/messy" and that using a QString method like number() or arg() seems so much easier. I can't keep repeating the same thing so I have said my piece now.

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by
                          #15

                          @JonB Agree!

                          1 Reply Last reply
                          1
                          • JoeCFDJ JoeCFD

                            @qAlexKo you can create one QString with arg.

                                int a = 1;
                                int b = 2;
                            
                                std::cout << qPrintable( QString( "%1." ).arg( a, 2, 10, QChar( '0' ) ) ) 
                                          << qPrintable( QString( "%1" ).arg( b, 2, 10, QChar( '0' ) ) ) << std::endl;
                            
                            Q Offline
                            Q Offline
                            qAlexKo
                            wrote on last edited by qAlexKo
                            #16

                            @JoeCFD said in QTextStream question:

                            std::cout << qPrintable( QString( "%1." ).arg( a, 2, 10, QChar( '0' ) ) ) 
                                      << qPrintable( QString( "%1" ).arg( b, 2, 10, QChar( '0' ) ) ) << std::endl;
                            

                            Compare your statement with mine if I define a simple macro:
                            #define co(width, what) qSetPadChar('0') << qSetFieldWidth(width) << what << qSetFieldWidth(0)

                            Then: "01.02" output is much simpler:
                            cout << co(2,a) << co(0,'.') <<co(2, b) << Qt::endl;

                            1 Reply Last reply
                            0

                            • Login

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