Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved] sprintf in qt
QtWS25 Last Chance

[Solved] sprintf in qt

Scheduled Pinned Locked Moved Mobile and Embedded
12 Posts 6 Posters 29.1k 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.
  • P Offline
    P Offline
    prabhuraj
    wrote on last edited by
    #1

    Hi all,
    I have been working on a code whare in I have use sprintf in Qt. When use the sprintf function in the following way

    @
    QString ::sprintf(wr_buff, "%s",tr_buff); //tr_buff contains a string and I want to put it into wr_buff
    @

    I get following error

    @
    error: no matching function for call to ‘QString::sprintf(QString&, const char [42], QString&).
    @

    The requirement of application is such that I have to use sprint and nothing else.

    Please help me rectify this problem.

    Thanks

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      According to the documentation, QString::sprintf has the format as the first argument, not as the second one.

      1 Reply Last reply
      0
      • AlicemirrorA Offline
        AlicemirrorA Offline
        Alicemirror
        wrote on last edited by
        #3

        Hi,

        I think that you should see on the Qt documentation for the api that create formatted strings. There are specific and complete functions / options for any kind of formatting reflecting the same options of sprintf.

        Enrico Miglino (aka Alicemirror)
        Balearic Dynamics
        Islas Baleares, Ibiza (Spain)
        www.balearicdynamics.com

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          QString::sprintf is not a static function. It alters the QString object. The correct way of using it is therefore:

          @QString str;
          // ...
          str.sprintf("%s", tr_buff);@

          or

          @QString str = QString().sprintf("%s", tr_buff);@

          However if tr_buff is a QString already (which the error suggests), just use
          @QString str = tr_buff;@

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          1
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            aditionally, sprintf is not a static member function.

            @
            QString wr_buff;
            char* tr_buff = "XXX";
            wr_buff.sprintf("%s",tr_buff);
            @

            But you should take care:

            bq. Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or arg(), both of which support Unicode strings seamlessly and are type-safe.

            "see documentation of QString::sprintf":http://doc.qt.nokia.com/4.7/qstring.html#sprintf

            If the requirement is to use sprint, are you sure, you shall do it with Qt? From my POV, it makes no sense, sorry...

            EDIT: too slow..., Gerolf

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              [quote author="Gerolf" date="1316155835"]If the requirement is to use sprint, are you sure, you shall do it with Qt? From my POV, it makes no sense, sorry...[/quote]I've come across some cases where you might want to use sprintf (e.g. formatted double precision numbers: %3.8lf is not possible with QString::arg(), but sometimes desirable). The problem OP shows isn't one of them.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • AlicemirrorA Offline
                AlicemirrorA Offline
                Alicemirror
                wrote on last edited by
                #7

                Btw, developing with Qt I think that the best practice is to use as possible the wide variety of api. At lest because grant one of the higher constan behavior on different platforms I have even see.

                Enrico Miglino (aka Alicemirror)
                Balearic Dynamics
                Islas Baleares, Ibiza (Spain)
                www.balearicdynamics.com

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  prabhuraj
                  wrote on last edited by
                  #8

                  Hey thanks a lot for all the replies. I finally got it working.
                  Thanks once again.

                  1 Reply Last reply
                  0
                  • AlicemirrorA Offline
                    AlicemirrorA Offline
                    Alicemirror
                    wrote on last edited by
                    #9

                    Very good. Ifthe problem is solved plese set your first post title with [Solved] in front. Thank you.

                    Enrico Miglino (aka Alicemirror)
                    Balearic Dynamics
                    Islas Baleares, Ibiza (Spain)
                    www.balearicdynamics.com

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      [quote author="Franzk" date="1316155969"]I've come across some cases where you might want to use sprintf (e.g. formatted double precision numbers: %3.8lf is not possible with QString::arg(), but sometimes desirable). The problem OP shows isn't one of them.
                      [/quote]

                      This works for me:

                      @
                      double x = 42.23;
                      QString xStr = QString("%1").arg(x, 3, 'f', 8);
                      @

                      Although the 3 in 3.8lf is superfluous, as the field width is 10 at least (the dot, eight digits after the dot and at least one digit before the dot).

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        Franzk
                        wrote on last edited by
                        #11

                        Ah nice. In that case sprintf can be avoided.

                        "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • AlicemirrorA Offline
                          AlicemirrorA Offline
                          Alicemirror
                          wrote on last edited by
                          #12

                          @Volker: prfectly agree with you. In Qt there are a lot of possibilities so a downgrade using sprintf is to be avoided at all. I don't use it by the middle of years '90 :)

                          Enrico Miglino (aka Alicemirror)
                          Balearic Dynamics
                          Islas Baleares, Ibiza (Spain)
                          www.balearicdynamics.com

                          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