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. How to use QString.arg
Servers for Qt installer are currently down

How to use QString.arg

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 5.9k 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.
  • W Offline
    W Offline
    Weichao Wang
    wrote on 15 Aug 2018, 09:06 last edited by
    #1

    Dear all,
    runningDate is a QDateTime object with the date value of 12.Aug.2018. With the following command
    cout << QString("%1").arg(runningDate.date().month(), 2, '0').toStdString() << endl;
    I expect the output "08", since fieldwidth is 2 and fillchar is 0. But the output is "8". How can integer 8 be converted into "08"?
    Weichao

    J 1 Reply Last reply 15 Aug 2018, 09:27
    0
    • W Offline
      W Offline
      Weichao Wang
      wrote on 15 Aug 2018, 09:26 last edited by
      #2

      Actually there are several static number() functions. But none of them contains fieldwidth and fillchar.

      1 Reply Last reply
      0
      • W Weichao Wang
        15 Aug 2018, 09:06

        Dear all,
        runningDate is a QDateTime object with the date value of 12.Aug.2018. With the following command
        cout << QString("%1").arg(runningDate.date().month(), 2, '0').toStdString() << endl;
        I expect the output "08", since fieldwidth is 2 and fillchar is 0. But the output is "8". How can integer 8 be converted into "08"?
        Weichao

        J Offline
        J Offline
        JonB
        wrote on 15 Aug 2018, 09:27 last edited by
        #3

        @Weichao-Wang
        I believe you are trying to use this overload:
        http://doc.qt.io/qt-5/qstring.html#arg-4
        QString QString::arg(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const

        So I make your parameters wrong. I think you're passing '0' as the base (so base 48!) and therefore ' ' as the fillChar, so I would guess you're actually seeing <space>8 as the output?

        Try:

        QString("%1").arg(runningDate.date().month(), 2, 10, '0').toStdString() 
        

        ?

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Weichao Wang
          wrote on 15 Aug 2018, 09:33 last edited by
          #4

          Now the output is 8.000000000... (some 30 '0').

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Weichao Wang
            wrote on 15 Aug 2018, 09:35 last edited by
            #5

            In the documentation we can find
            QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
            It contains only 3 parameters.
            I'm using Qt 4.6.

            J 1 Reply Last reply 15 Aug 2018, 09:45
            0
            • J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 15 Aug 2018, 09:37 last edited by
              #6

              @Weichao-Wang why reinventing the wheel?

              cout << runningDate.toString("MM").toStdString() << endl;


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              W 1 Reply Last reply 15 Aug 2018, 10:01
              3
              • W Offline
                W Offline
                Weichao Wang
                wrote on 15 Aug 2018, 09:41 last edited by
                #7

                Now I've changed the line into following and the output is 08:
                cout << QString("%1").arg(QString::number(runningDate.date().month()), 2, '0').toStdString() << endl;
                I miss something like:
                static QString QString::number(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' '))
                which would convert an interger into a string with desired length and fillchar without creating a QString object.

                1 Reply Last reply
                0
                • W Weichao Wang
                  15 Aug 2018, 09:35

                  In the documentation we can find
                  QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
                  It contains only 3 parameters.
                  I'm using Qt 4.6.

                  J Offline
                  J Offline
                  JonB
                  wrote on 15 Aug 2018, 09:45 last edited by JonB
                  #8

                  @Weichao-Wang said in How to use QString.arg:

                  In the documentation we can find
                  QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
                  It contains only 3 parameters.
                  I'm using Qt 4.6.

                  Your code reads:

                  QString("%1").arg(runningDate.date().month(), 2, '0')
                  

                  QDate::date().month() returns an int not a QString. So how do you figure it is calling your overload with const QString & a?

                  W 1 Reply Last reply 15 Aug 2018, 10:04
                  0
                  • J J.Hilk
                    15 Aug 2018, 09:37

                    @Weichao-Wang why reinventing the wheel?

                    cout << runningDate.toString("MM").toStdString() << endl;

                    W Offline
                    W Offline
                    Weichao Wang
                    wrote on 15 Aug 2018, 10:01 last edited by
                    #9

                    @J.Hilk
                    this is ingenious! Thank you!
                    Actually I need "20180803" for 3.Aug.2018. With date.toString("yyyyMMdd") I've solved the problem with one stroke.
                    But for a normal integer i can we only use the following or is there a simpler conversion?
                    QString("%1").arg(QString::number(i), 2, '0' )

                    J 1 Reply Last reply 15 Aug 2018, 10:05
                    0
                    • J JonB
                      15 Aug 2018, 09:45

                      @Weichao-Wang said in How to use QString.arg:

                      In the documentation we can find
                      QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
                      It contains only 3 parameters.
                      I'm using Qt 4.6.

                      Your code reads:

                      QString("%1").arg(runningDate.date().month(), 2, '0')
                      

                      QDate::date().month() returns an int not a QString. So how do you figure it is calling your overload with const QString & a?

                      W Offline
                      W Offline
                      Weichao Wang
                      wrote on 15 Aug 2018, 10:04 last edited by
                      #10

                      @JonB said in How to use QString.arg:

                      @Weichao-Wang said in How to use QString.arg:

                      In the documentation we can find
                      QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
                      It contains only 3 parameters.
                      I'm using Qt 4.6.

                      Your code reads:

                      QString("%1").arg(runningDate.date().month(), 2, '0')
                      

                      QDate::date().month() returns an int not a QString. So how do you figure it is calling your overload with const QString & a?

                      Yes, I've noticed the problem and changed it to
                      QString("%1").arg(QString::number(i), 2, '0')

                      1 Reply Last reply
                      0
                      • W Weichao Wang
                        15 Aug 2018, 10:01

                        @J.Hilk
                        this is ingenious! Thank you!
                        Actually I need "20180803" for 3.Aug.2018. With date.toString("yyyyMMdd") I've solved the problem with one stroke.
                        But for a normal integer i can we only use the following or is there a simpler conversion?
                        QString("%1").arg(QString::number(i), 2, '0' )

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 15 Aug 2018, 10:05 last edited by
                        #11

                        @Weichao-Wang like @JonB said all overloads of arg that accept an int want a base to convert it to string

                        QString("%1").arg(QString::number(i), 2, '0' );
                        ==
                        QString("%1").arg(i, 2, 10, QChar('0') );


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        3

                        1/11

                        15 Aug 2018, 09:06

                        • Login

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