PDA

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



hakermania
5th January 2011, 23:19
QProgressbar Format can be set as follow:

progressbar->setFormat("text here");
What I want is to have some text into the setFormat(); and an integer t follow.
Specifically, I need

ui->progressbar->setFormat("Starting with " + ui->listwidget->count()+ " files");

I get error

invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’

So, how will I do what I want?

Thx for any replies, happy new year.

SixDegrees
5th January 2011, 23:30
Use QStrings.

Zlatomir
5th January 2011, 23:34
You can use the arg(..) (http://doc.qt.nokia.com/latest/qstring.html#arg-10) function, something like this should do the trick:


ui->progressbar->setFormat(QString("Starting with %1 files").arg(ui->listwidget->count()) );

nroberts
5th January 2011, 23:42
QProgressbar Format can be set as follow:

progressbar->setFormat("text here");
What I want is to have some text into the setFormat(); and an integer t follow.
Specifically, I need

ui->progressbar->setFormat("Starting with " + ui->listwidget->count()+ " files");

I get error

invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’


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 +:



QString("xxx") + y + QString("snatoheusn")


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



std::string str = boost::str(boost::format("xxx %d xxx") % i);


There's more... What you specifically need is hard to say. It sounds rather that the .arg() method might be best for you.

wysota
6th January 2011, 14:25
I get error

invalid operands of types ‘const char*’ and ‘const char [7]’ to binary ‘operator+’

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.

hakermania
6th January 2011, 21:54
Thx for your replies! :):rolleyes:

MarekR22
7th January 2011, 08:31
Hi. IMHO the best approach in your case is:

ui->progressbar->setFormat(tr("Starting with %n files", "", ui->listwidget->count()));

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-source-translation.html