Results 1 to 3 of 3

Thread: NUll character output for combobox index

  1. #1
    Join Date
    Oct 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default NUll character output for combobox index

    Whenever I run this bit of code it prints blank characters:

    Qt Code:
    1. void Photo::apertureChanged()
    2. {
    3.  
    4. x = aperture.itemText(0);
    5. QByteArray i = x.toLatin1();
    6. const char *c_str2 = i.data();
    7. printf(c_str2);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    when really its supposed to print the aperture number of combobox index 0.
    it compiles fine.
    Thanks in Advance
    Last edited by jpn; 5th October 2008 at 13:25. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2006
    Location
    San Antonio, TX
    Posts
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: NUll character output for combobox index

    mdskpr778,

    try:
    Qt Code:
    1. void Photo::apertureChanged()
    2. {
    3. x = aperture.itemText(0);
    4. if( !x.isEmpty() ){
    5. const char *c_str2 = x.toLatin1().data();
    6. printf(c_str2);
    7. } else {
    8. printf( "x is empty\n" );
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: NUll character output for combobox index

    These two lines will most likely crash your application:
    Qt Code:
    1. const char *c_str2 = x.toLatin1().data();
    2. printf(c_str2);
    To copy to clipboard, switch view to plain text mode 
    Use:
    Qt Code:
    1. printf(x.toLatin1().constData());
    To copy to clipboard, switch view to plain text mode 
    That has to be the single most common Qt mistake.

Similar Threads

  1. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  2. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  3. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10
  4. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 08:07
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.