Results 1 to 3 of 3

Thread: Function returns a gibberish value

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26
    Thanked 1 Time in 1 Post

    Default Function returns a gibberish value

    Hi everyone, I have the function below. the function is suppose to generate an account number and return it in form of a QString.
    Qt Code:
    1. QString NewAccount::genAccountNum()
    2. {
    3. QString tempStr, accNum;
    4.  
    5. int num1 = rand()%100000 + 999999;
    6. tempStr.append(static_cast<QString>(num1));
    7. accNum.append("AB");
    8. accNum.append(tempStr);
    9.  
    10. return accNum;
    11. }
    To copy to clipboard, switch view to plain text mode 

    The function returns the substring "AB" followed by gibberish, something that looks like Chinese fonts. AM I DOING SOMETHING WRONG HERE?

  2. #2
    Join Date
    Oct 2013
    Posts
    41
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 8 Times in 7 Posts

    Default Re: Function returns a gibberish value

    I would use:
    tempStr.append(QString::number(num1));

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Function returns a gibberish value

    As sulliwk06 says the function you want is QString::number() or QString::arg(). There is no meaningful way to cast an int to QString, so when you forced the compiler to do it anyway what you ended up with was something that will sometimes give rubbish and sometimes crash your program.

Similar Threads

  1. Server sending gibberish to the client.
    By ayanda83 in forum Newbie
    Replies: 1
    Last Post: 12th May 2013, 19:00
  2. Replies: 11
    Last Post: 5th September 2012, 21:47
  3. QFont::macFontID function always returns 0
    By ironexmaiden in forum Qt Programming
    Replies: 0
    Last Post: 22nd February 2012, 09:23
  4. Replies: 3
    Last Post: 26th September 2011, 09:20
  5. function returns QString, usage generates segfault
    By space_otter in forum General Programming
    Replies: 5
    Last Post: 8th March 2010, 16:54

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
  •  
Qt is a trademark of The Qt Company.