Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] sprintf in qt

    Mobile and Embedded
    6
    12
    26871
    Loading More Posts
    • 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
      prabhuraj last edited by

      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 Reply Quote 0
      • A
        andre last edited by

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

        1 Reply Last reply Reply Quote 0
        • Alicemirror
          Alicemirror last edited by

          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 Reply Quote 0
          • F
            Franzk last edited by

            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 Reply Quote 1
            • G
              giesbert last edited by

              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 Reply Quote 0
              • F
                Franzk last edited by

                [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 Reply Quote 0
                • Alicemirror
                  Alicemirror last edited by

                  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 Reply Quote 0
                  • P
                    prabhuraj last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • Alicemirror
                      Alicemirror last edited by

                      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 Reply Quote 0
                      • G
                        goetz last edited by

                        [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 Reply Quote 0
                        • F
                          Franzk last edited by

                          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 Reply Quote 0
                          • Alicemirror
                            Alicemirror last edited by

                            @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 Reply Quote 0
                            • First post
                              Last post