Results 1 to 7 of 7

Thread: QString display error

  1. #1
    Join Date
    Jul 2009
    Posts
    55
    Thanks
    3

    Default QString display error

    is there anything wrong with the following code? how come the %s display a square insstead of the word i want??

    Qt Code:
    1. if (nodeData.addr==1)
    2. {
    3. nodeName = "room";
    4. }
    5. else if (nodeData.addr==2)
    6. {
    7. nodeName = "bed";
    8. }
    9. else if (nodeData.addr==3)
    10. {
    11. nodeName = "kitchen";
    12. }
    13. else
    14. {
    15. nodeName = "End Device";
    16. }
    17.  
    18. if (nodeData.addr==0) //node 0
    19. {
    20. // Write node number
    21. //qsTemp.sprintf("Node: %d", nodeData.addr);
    22. qsTemp.sprintf("Access Point");
    23. //xpainter->setFont(addrFont);
    24. painter->setFont(timeFont);
    25. painter->drawText(-50,-15,100,20,Qt::AlignCenter , qsTemp,0);
    26. }
    27. else
    28. {
    29. // Write node number
    30. qsTemp.sprintf("%s %d" ,nodeName, nodeData.addr );
    31. //xpainter->setFont(addrFont);
    32. painter->setFont(timeFont);
    33. painter->drawText(-50,-22,100,20,Qt::AlignCenter , qsTemp,0);
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString display error

    Use this instead.
    QString::arg
    Exmaple:
    Qt Code:
    1. QString i; // current file's number
    2. QString total; // number of files to process
    3. QString fileName; // current file's name
    4. QString status = QString("Processing file %1 of %2: %3")
    5. .arg(i).arg(total).arg(fileName);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Posts
    55
    Thanks
    3

    Default Re: QString display error

    Quote Originally Posted by yogeshgokul View Post
    Use this instead.


    Exmaple:
    Qt Code:
    1. QString i; // current file's number
    2. QString total; // number of files to process
    3. QString fileName; // current file's name
    4. QString status = QString("Processing file %1 of %2: %3")
    5. .arg(i).arg(total).arg(fileName);
    To copy to clipboard, switch view to plain text mode 
    i dun see how can qstring::arg applied to my code..

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString display error

    Quote Originally Posted by Devoraz View Post
    i dun see how can qstring::arg applied to my code..
    Replace sprintf with QString::arg.

  5. The following user says thank you to yogeshgokul for this useful post:

    Devoraz (5th August 2009)

  6. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString display error

    Qt Code:
    1. else
    2. {
    3. // Write node number
    4. qsTemp.sprintf("%s %d" ,nodeName, nodeData.addr );
    5. //xpainter->setFont(addrFont);
    6. painter->setFont(timeFont);
    7. painter->drawText(-50,-22,100,20,Qt::AlignCenter , qsTemp,0);
    8. }
    To copy to clipboard, switch view to plain text mode 

    this can be like this

    Qt Code:
    1. QString value = QString("Name:%1 and Address:%2").arg(nodeName).arg(QString::number(nodeData.addr, 16));
    2. painter->drawText(-50,-22,100,20,Qt::AlignCenter , value,0);
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  7. The following user says thank you to wagmare for this useful post:

    Devoraz (5th August 2009)

  8. #6
    Join Date
    Jul 2009
    Posts
    55
    Thanks
    3

    Default Re: QString display error

    Quote Originally Posted by yogeshgokul View Post
    Replace sprintf with QString::arg.
    Qt Code:
    1. if (nodeData.addr==1)
    2. {
    3. nodeName = "room";
    4. }
    5. else if (nodeData.addr==2)
    6. {
    7. nodeName = "bed";
    8. }
    9. else if (nodeData.addr==3)
    10. {
    11. nodeName = "kitchen";
    12. }
    13. else
    14. {
    15. nodeName = "End Device";
    16. }
    To copy to clipboard, switch view to plain text mode 

    the if else statement share the same variable QString nodeName, u sure the %1 can work? i tried it cant work.

  9. #7
    Join Date
    Jul 2009
    Posts
    55
    Thanks
    3

    Default Re: QString display error

    Quote Originally Posted by wagmare View Post
    Qt Code:
    1. else
    2. {
    3. // Write node number
    4. qsTemp.sprintf("%s %d" ,nodeName, nodeData.addr );
    5. //xpainter->setFont(addrFont);
    6. painter->setFont(timeFont);
    7. painter->drawText(-50,-22,100,20,Qt::AlignCenter , qsTemp,0);
    8. }
    To copy to clipboard, switch view to plain text mode 

    this can be like this

    Qt Code:
    1. QString value = QString("Name:%1 and Address:%2").arg(nodeName).arg(QString::number(nodeData.addr, 16));
    2. painter->drawText(-50,-22,100,20,Qt::AlignCenter , value,0);
    To copy to clipboard, switch view to plain text mode 
    OMG~ it works. thanks!! my code work as well.

    Qt Code:
    1. QString value = QString("%1 %2").arg(nodeName).arg(nodeData.addr);
    2. painter->setFont(timeFont);
    3. painter->drawText(-50,-22,100,20,Qt::AlignCenter , value,0);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  4. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.