Results 1 to 7 of 7

Thread: Simple Question about how to put an int to a function that requires coonst QString.

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Simple Question about how to put an int to a function that requires coonst QString.

    QProgressbar Format can be set as follow:
    Qt Code:
    1. progressbar->setFormat("text here");
    To copy to clipboard, switch view to plain text mode 
    What I want is to have some text into the setFormat(); and an integer t follow.
    Specifically, I need
    Qt Code:
    1. ui->progressbar->setFormat("Starting with " + ui->listwidget->count()+ " files");
    To copy to clipboard, switch view to plain text mode 

    I get error
    Qt Code:
    1. invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’
    To copy to clipboard, switch view to plain text mode 

    So, how will I do what I want?

    Thx for any replies, happy new year.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Simple Question about how to put an int to a function that requires coonst QStrin

    Use QStrings.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Simple Question about how to put an int to a function that requires coonst QStrin

    You can use the arg(..) function, something like this should do the trick:
    Qt Code:
    1. ui->progressbar->setFormat(QString("Starting with %1 files").arg(ui->listwidget->count()) );
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to Zlatomir for this useful post:

    hakermania (6th January 2011)

  5. #4
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple Question about how to put an int to a function that requires coonst QStrin

    Quote Originally Posted by hakermania View Post
    QProgressbar Format can be set as follow:
    Qt Code:
    1. progressbar->setFormat("text here");
    To copy to clipboard, switch view to plain text mode 
    What I want is to have some text into the setFormat(); and an integer t follow.
    Specifically, I need
    Qt Code:
    1. ui->progressbar->setFormat("Starting with " + ui->listwidget->count()+ " files");
    To copy to clipboard, switch view to plain text mode 

    I get error
    Qt Code:
    1. invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’
    To copy to clipboard, switch view to plain text mode 
    Correct. The + operator doesn't work on C-style strings the way you'd want (it adds the pointers when it pretends to work--you're lucky it failed to compile). Operator + does work on a lot of string classes though, such as QString or std::string.

    So, how will I do what I want?
    .
    You could make sure both "" strings are turned into QStrings before you try the +:

    Qt Code:
    1. QString("xxx") + y + QString("snatoheusn")
    To copy to clipboard, switch view to plain text mode 

    You could do the arg method shown by Zlatomir.

    You could use std::string similar to how I show QString above.

    You could wrap in tr() instead of QString().

    You could use boost::format

    Qt Code:
    1. std::string str = boost::str(boost::format("xxx %d xxx") % i);
    To copy to clipboard, switch view to plain text mode 

    There's more... What you specifically need is hard to say. It sounds rather that the .arg() method might be best for you.
    This rude guy who doesn't want you to answer his questions.

    Note: An "expert" here is just someone that's posted a lot.

    "The fact of where you do the encapsulation is meaningless." - Qt Certified Developer and forum moderator

  6. The following user says thank you to nroberts for this useful post:

    hakermania (6th January 2011)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple Question about how to put an int to a function that requires coonst QStrin

    Quote Originally Posted by hakermania View Post
    I get error
    Qt Code:
    1. invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’
    To copy to clipboard, switch view to plain text mode 

    So, how will I do what I want?
    Note that the error is a little misleading. Although it complains about character strings, the real problem is the integer you are passing somewhere in the middle.

    As for the + operator, it is good to always wrap all C strings into QLatin1String. It's a bit of more work but it has some benefits for more complex applications. There is not operator+ defined for it though, so it's likely you'd get some different error then too but at least the compiler wouldn't try to add an integer to your string.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    hakermania (6th January 2011)

  9. #6
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simple Question about how to put an int to a function that requires coonst QStrin

    Thx for your replies!
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  10. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Simple Question about how to put an int to a function that requires coonst QStrin

    Hi. IMHO the best approach in your case is:
    Qt Code:
    1. ui->progressbar->setFormat(tr("Starting with %n files", "", ui->listwidget->count()));
    To copy to clipboard, switch view to plain text mode 

    This will allow to alter translation depending on number value.
    http://doc.trolltech.com/4.7/qobject.html#tr
    http://doc.trolltech.com/4.7/i18n-so...anslation.html
    Last edited by MarekR22; 7th January 2011 at 08:32. Reason: reformatted to look better

Similar Threads

  1. QString function by value
    By hmarani in forum Newbie
    Replies: 6
    Last Post: 24th November 2010, 10:01
  2. call function as Qstring
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 01:35
  3. Method/Function With QString Arg
    By seanmu13 in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2007, 09:43
  4. simple QString comment
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2006, 13:29
  5. Using QString in C function ...
    By Godlike in forum Newbie
    Replies: 10
    Last Post: 8th April 2006, 17:01

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.