Results 1 to 2 of 2

Thread: QString mystery

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QString mystery

    Hi

    I've used QString many many times before but the following does not make any sense to me. I have this code:

    Qt Code:
    1. QString version;
    2. QString seperator = QString(".");
    3. int minor = 0;
    4. int major = 0;
    5. if (minor <= 9)
    6. version = QString("%1%200%3").arg(major).arg(seperator).arg(minor);
    7. else if (minor <= 99)
    8. version = QString("%1%20%3").arg(major).arg(seperator).arg(minor);
    9. else
    10. version = QString("%1%2%3").arg(major).arg(seperator).arg(minor);
    11.  
    12. qDebug() << seperator;
    13. qDebug() << version;
    To copy to clipboard, switch view to plain text mode 

    Amazingly the qDebug() outputs gives:
    Qt Code:
    1. "."
    2. "000."
    To copy to clipboard, switch view to plain text mode 

    While the result should clearly be "0.000"

    If anyone can give an explanation why version is wrong, I would really appreciate it.
    Thanks,
    Jac


    Added after 12 minutes:


    Solved it:

    Qt Code:
    1. QString version = QString::number(major) + QString(".%1").arg(minor,3,10,QChar('0'));
    To copy to clipboard, switch view to plain text mode 
    Last edited by JPNaude; 19th January 2011 at 08:35.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString mystery

    Try this:
    Qt Code:
    1. QString version;
    2. QString seperator = QString(".");
    3. int minor = 0;
    4. int major = 0;
    5. if (minor <= 9)
    6. version = QString("%1%200%3").arg(QString::number(major)).arg(seperator).arg(QString::number(minor));
    7. ...//etc
    8. qDebug() << seperator;
    9. qDebug() << version;
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    JPNaude (19th January 2011)

Similar Threads

  1. Replies: 8
    Last Post: 25th November 2010, 11:40
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. Replies: 2
    Last Post: 24th April 2009, 12:13
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.