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. Justify QString line for debug
Forum Updated to NodeBB v4.3 + New Features

Justify QString line for debug

Scheduled Pinned Locked Moved Solved Mobile and Embedded
18 Posts 6 Posters 3.5k Views 3 Watching
  • 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.
  • K Offline
    K Offline
    Kashif
    wrote on 24 May 2018, 09:04 last edited by
    #1

    Hi All,

    I want to know is there any trick we can justify QString text to a defined number of width, a sample standard c program looks like:

    void f(char *s)
    {
         char mychar[50] = {0};
            sprintf( mychar, "---%*s%*s---\n",10+strlen(s)/2,s,10-strlen(s)/2,"");
            printf("%s", mychar);
    }
    
    int main() {
            f("uno");
            f("quattro");
    	getch();
    	return 0;
    }
    
    //will result with 20 characters width like
    ---        uno         ---
    ---      quattro       ---
    

    Thanks

    M 1 Reply Last reply 24 May 2018, 09:51
    0
    • K kenchan
      3 Jun 2018, 08:02

      @Kashif
      Well, if you want to be a stickler about coding. Since Qt is not a language but a framework built with C++ you can't avoid mixing C++ code with Qt :-).

      K Offline
      K Offline
      Kashif
      wrote on 3 Jun 2018, 10:13 last edited by
      #18

      Hi @kenchan,
      Yes I know that QT is a framework (apologies on above statement saying languages) and many are things similar like C++11 features, but as said we are working on payment device (card acceptance devices) we mostly use generic C. Mixing the code of C with C++ then with QT framework sometimes is difficult to understand even for me (I am not fully trained in QT but have attended hands-on training of Nokia international training program almost 8 years ago), so it can be a hard thing for any new team member to work on devices.

      Thanks

      1 Reply Last reply
      0
      • K Kashif
        24 May 2018, 09:04

        Hi All,

        I want to know is there any trick we can justify QString text to a defined number of width, a sample standard c program looks like:

        void f(char *s)
        {
             char mychar[50] = {0};
                sprintf( mychar, "---%*s%*s---\n",10+strlen(s)/2,s,10-strlen(s)/2,"");
                printf("%s", mychar);
        }
        
        int main() {
                f("uno");
                f("quattro");
        	getch();
        	return 0;
        }
        
        //will result with 20 characters width like
        ---        uno         ---
        ---      quattro       ---
        

        Thanks

        M Offline
        M Offline
        mvuori
        wrote on 24 May 2018, 09:51 last edited by
        #2

        What's this got to do with mobile & embedding?

        The trick is called "programming". Just port that code to Qt. There are QString::sprintf(), QString::toAscii() etc... that can be used to build similar function.

        K 1 Reply Last reply 24 May 2018, 10:32
        2
        • M mvuori
          24 May 2018, 09:51

          What's this got to do with mobile & embedding?

          The trick is called "programming". Just port that code to Qt. There are QString::sprintf(), QString::toAscii() etc... that can be used to build similar function.

          K Offline
          K Offline
          Kashif
          wrote on 24 May 2018, 10:32 last edited by
          #3

          Hi @mvuori,
          FYI, I am working on embedded device that some functions are not available in embedded systems this is the reason I posted in Mobile & embedded.
          Second, your solution is using slandered c array whereas I have asked to justify QString itself. Suppose I got configuration from a file in QString, trimmed the string to remove white spaces, now I have to send it to a display device but in justified format LEFT, CENTER, RIGHT.

          Hope you understand the requirement/logic behind my question.

          Thanks

          J K 2 Replies Last reply 24 May 2018, 11:09
          0
          • K Kashif
            24 May 2018, 10:32

            Hi @mvuori,
            FYI, I am working on embedded device that some functions are not available in embedded systems this is the reason I posted in Mobile & embedded.
            Second, your solution is using slandered c array whereas I have asked to justify QString itself. Suppose I got configuration from a file in QString, trimmed the string to remove white spaces, now I have to send it to a display device but in justified format LEFT, CENTER, RIGHT.

            Hope you understand the requirement/logic behind my question.

            Thanks

            J Online
            J Online
            JonB
            wrote on 24 May 2018, 11:09 last edited by
            #4

            @Kashif
            If you are happy with C's sprintf, as @mvuori said there is e.g. http://doc.qt.io/qt-5/qstring.html#asprintf or various http://doc.qt.io/qt-5/qstring.html#arg which will work with/return QStrings.

            1 Reply Last reply
            0
            • K Kashif
              24 May 2018, 10:32

              Hi @mvuori,
              FYI, I am working on embedded device that some functions are not available in embedded systems this is the reason I posted in Mobile & embedded.
              Second, your solution is using slandered c array whereas I have asked to justify QString itself. Suppose I got configuration from a file in QString, trimmed the string to remove white spaces, now I have to send it to a display device but in justified format LEFT, CENTER, RIGHT.

              Hope you understand the requirement/logic behind my question.

              Thanks

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 24 May 2018, 11:09 last edited by
              #5

              QString doesn't have such stuff, but then again, it shouldn't really as this doesn't pertain to the string/text itself, but rather to the way it's output on a device/terminal. Try with QTextStream - QTextStream::setFieldWidth and QTextStream::setFieldAlignment

              Read and abide by the Qt Code of Conduct

              J 1 Reply Last reply 24 May 2018, 11:10
              0
              • K kshegunov
                24 May 2018, 11:09

                QString doesn't have such stuff, but then again, it shouldn't really as this doesn't pertain to the string/text itself, but rather to the way it's output on a device/terminal. Try with QTextStream - QTextStream::setFieldWidth and QTextStream::setFieldAlignment

                J Online
                J Online
                JonB
                wrote on 24 May 2018, 11:10 last edited by
                #6

                @kshegunov
                Our replies crossed! Given my reply, in what way "QString doesn't have such stuff," ? :)

                K 1 Reply Last reply 24 May 2018, 11:13
                0
                • J JonB
                  24 May 2018, 11:10

                  @kshegunov
                  Our replies crossed! Given my reply, in what way "QString doesn't have such stuff," ? :)

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 24 May 2018, 11:13 last edited by kshegunov
                  #7

                  @JonB said in Justify QString line for debug:

                  Our replies crossed!

                  True. The wonders of network latency ... ;)

                  Given my reply, in what way "QString doesn't have such stuff," ? :)

                  I meant it's not really designed to format/reformat strings. While QTextStream is exactly for that, it has manipulators and properties, and settings that are all connected to HOW it is displayed, not as with QString, where it's WHAT it is displayed. :)

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • P Offline
                    P Offline
                    Padlock
                    wrote on 29 May 2018, 22:39 last edited by
                    #8

                    Why not just keep the C function and wrap it with a QString?
                    Qstring.
                    http://doc.qt.io/qt-5/qstring.html#fromUtf8

                    K 1 Reply Last reply 30 May 2018, 05:48
                    0
                    • P Padlock
                      29 May 2018, 22:39

                      Why not just keep the C function and wrap it with a QString?
                      Qstring.
                      http://doc.qt.io/qt-5/qstring.html#fromUtf8

                      K Offline
                      K Offline
                      Kashif
                      wrote on 30 May 2018, 05:48 last edited by
                      #9

                      Hi @Padlock,
                      I already did this, the only thing is I have to remove whitespaces using standard c in a loop.

                      Thanks

                      J 1 Reply Last reply 30 May 2018, 07:43
                      0
                      • K Kashif
                        30 May 2018, 05:48

                        Hi @Padlock,
                        I already did this, the only thing is I have to remove whitespaces using standard c in a loop.

                        Thanks

                        J Online
                        J Online
                        JonB
                        wrote on 30 May 2018, 07:43 last edited by
                        #10

                        @Kashif
                        What whitespaces do you have to remove from what how?

                        K 1 Reply Last reply 30 May 2018, 09:14
                        0
                        • J JonB
                          30 May 2018, 07:43

                          @Kashif
                          What whitespaces do you have to remove from what how?

                          K Offline
                          K Offline
                          Kashif
                          wrote on 30 May 2018, 09:14 last edited by
                          #11

                          Hi @JonB,

                          I am creating a payment application (Credit/Debit cards), that gets dynamic configuration ex: Receipt headers from configuration that can have white spaces. To adjust the transaction receipt I have to remove white spaces: ex:

                          Normal
                          Header1:  "TEST MERCHANT ABC       "
                          Header2:  "LOCATION #1, TERMINAL #1"
                          Line3:    "CARD: 441000XXXXXX0001  "
                          ~##################################~
                          Formatted
                          Header1:  "   TEST MERCHANT ABC    "
                          Header1:  "LOCATION #1, TERMINAL #1"
                          Line3:    "CARD: 441000XXXXXX0001  "
                          

                          You can notice the header line 1 is formatted to center justified, hope this clears your doubt.

                          Thanks

                          J 1 Reply Last reply 30 May 2018, 10:54
                          0
                          • K Kashif
                            30 May 2018, 09:14

                            Hi @JonB,

                            I am creating a payment application (Credit/Debit cards), that gets dynamic configuration ex: Receipt headers from configuration that can have white spaces. To adjust the transaction receipt I have to remove white spaces: ex:

                            Normal
                            Header1:  "TEST MERCHANT ABC       "
                            Header2:  "LOCATION #1, TERMINAL #1"
                            Line3:    "CARD: 441000XXXXXX0001  "
                            ~##################################~
                            Formatted
                            Header1:  "   TEST MERCHANT ABC    "
                            Header1:  "LOCATION #1, TERMINAL #1"
                            Line3:    "CARD: 441000XXXXXX0001  "
                            

                            You can notice the header line 1 is formatted to center justified, hope this clears your doubt.

                            Thanks

                            J Online
                            J Online
                            JonB
                            wrote on 30 May 2018, 10:54 last edited by
                            #12

                            @Kashif

                            I have to remove whitespaces using standard c in a loop.

                            Well, you don't have to do it that way, e.g. http://doc.qt.io/qt-5/qstring.html#trimmed will save you the trouble of some C loop.

                            K 1 Reply Last reply 30 May 2018, 11:22
                            0
                            • J JonB
                              30 May 2018, 10:54

                              @Kashif

                              I have to remove whitespaces using standard c in a loop.

                              Well, you don't have to do it that way, e.g. http://doc.qt.io/qt-5/qstring.html#trimmed will save you the trouble of some C loop.

                              K Offline
                              K Offline
                              Kashif
                              wrote on 30 May 2018, 11:22 last edited by
                              #13

                              Hi @JonB,

                              Yes but with QString it is not possible to justify text (the main question). so converting char[] into QString and then QString into char[] will make my code look very bad this is the reason I followed the standard C and removed the white spaces using 3 lines of code.

                              Thanks

                              J P 2 Replies Last reply 30 May 2018, 14:07
                              0
                              • K Kashif
                                30 May 2018, 11:22

                                Hi @JonB,

                                Yes but with QString it is not possible to justify text (the main question). so converting char[] into QString and then QString into char[] will make my code look very bad this is the reason I followed the standard C and removed the white spaces using 3 lines of code.

                                Thanks

                                J Online
                                J Online
                                JonB
                                wrote on 30 May 2018, 14:07 last edited by
                                #14

                                @Kashif
                                There's no reason to use any char[] instead of QString in the first place. But never mind.

                                1 Reply Last reply
                                1
                                • K Kashif
                                  30 May 2018, 11:22

                                  Hi @JonB,

                                  Yes but with QString it is not possible to justify text (the main question). so converting char[] into QString and then QString into char[] will make my code look very bad this is the reason I followed the standard C and removed the white spaces using 3 lines of code.

                                  Thanks

                                  P Offline
                                  P Offline
                                  Padlock
                                  wrote on 30 May 2018, 22:24 last edited by
                                  #15

                                  @Kashif
                                  "Makes code look very bad" is a deceptive phrase. Looking at your original example in C, that's not just deceptive but may leave a gaping security hole in your application. Considering that this is meant for some payment stuff, I'd say that's terribly scary.

                                  Just make a copy of the QString, then trim white spaces at the start and end as has been suggested, compare the lengths, and add half of the difference to each side of the new string as spaces. Since we're talking about a few hundred CPU cycles only I don't think there's a difference between the C and the QString implementation when it comes to speed.

                                  K 1 Reply Last reply 3 Jun 2018, 06:59
                                  2
                                  • P Padlock
                                    30 May 2018, 22:24

                                    @Kashif
                                    "Makes code look very bad" is a deceptive phrase. Looking at your original example in C, that's not just deceptive but may leave a gaping security hole in your application. Considering that this is meant for some payment stuff, I'd say that's terribly scary.

                                    Just make a copy of the QString, then trim white spaces at the start and end as has been suggested, compare the lengths, and add half of the difference to each side of the new string as spaces. Since we're talking about a few hundred CPU cycles only I don't think there's a difference between the C and the QString implementation when it comes to speed.

                                    K Offline
                                    K Offline
                                    Kashif
                                    wrote on 3 Jun 2018, 06:59 last edited by
                                    #16

                                    Hi @Padlock,
                                    The code in first post is a sample code, not the exact of my application. Second, the code is used for display/printer string building (for the thermal printer used in payment devices) so it does not have anything to do with actual payment code.
                                    Second, as a developer I prefer my code to be simple if not possible then use one language (either QT, C++, C) rather to mix multiple things so that one will be taking care the code in future should not have complex understanding as we are dealing with payment application so one single mistake can make huge errors (ex: you make purchase of $100.00 but application approves your credit card for $1000.00).

                                    If working on PC or Server with huge amount of RAM and storage then we can use any technique/language like Java, dotNet without thinking much on pointers and arrays but on a specific payment device (that is not as powerful as mobile) we can not experiment mixing multiple technologies that may lead to some disaster for us, customer and end users.

                                    Thanks

                                    K 1 Reply Last reply 3 Jun 2018, 08:02
                                    0
                                    • K Kashif
                                      3 Jun 2018, 06:59

                                      Hi @Padlock,
                                      The code in first post is a sample code, not the exact of my application. Second, the code is used for display/printer string building (for the thermal printer used in payment devices) so it does not have anything to do with actual payment code.
                                      Second, as a developer I prefer my code to be simple if not possible then use one language (either QT, C++, C) rather to mix multiple things so that one will be taking care the code in future should not have complex understanding as we are dealing with payment application so one single mistake can make huge errors (ex: you make purchase of $100.00 but application approves your credit card for $1000.00).

                                      If working on PC or Server with huge amount of RAM and storage then we can use any technique/language like Java, dotNet without thinking much on pointers and arrays but on a specific payment device (that is not as powerful as mobile) we can not experiment mixing multiple technologies that may lead to some disaster for us, customer and end users.

                                      Thanks

                                      K Offline
                                      K Offline
                                      kenchan
                                      wrote on 3 Jun 2018, 08:02 last edited by
                                      #17

                                      @Kashif
                                      Well, if you want to be a stickler about coding. Since Qt is not a language but a framework built with C++ you can't avoid mixing C++ code with Qt :-).

                                      K 1 Reply Last reply 3 Jun 2018, 10:13
                                      1
                                      • K kenchan
                                        3 Jun 2018, 08:02

                                        @Kashif
                                        Well, if you want to be a stickler about coding. Since Qt is not a language but a framework built with C++ you can't avoid mixing C++ code with Qt :-).

                                        K Offline
                                        K Offline
                                        Kashif
                                        wrote on 3 Jun 2018, 10:13 last edited by
                                        #18

                                        Hi @kenchan,
                                        Yes I know that QT is a framework (apologies on above statement saying languages) and many are things similar like C++11 features, but as said we are working on payment device (card acceptance devices) we mostly use generic C. Mixing the code of C with C++ then with QT framework sometimes is difficult to understand even for me (I am not fully trained in QT but have attended hands-on training of Nokia international training program almost 8 years ago), so it can be a hard thing for any new team member to work on devices.

                                        Thanks

                                        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