Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Justify QString line for debug



  • 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



  • 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



  • 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.



  • 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



  • @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.


  • Qt Champions 2017

    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



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


  • Qt Champions 2017

    @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. :)



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



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

    Thanks



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



  • 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



  • @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.



  • 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



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



  • @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.



  • 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



  • @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 :-).



  • 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


Log in to reply