Simple Question about how to put an int to a function that requires coonst QString.
QProgressbar Format can be set as follow:
Code:
progressbar->setFormat("text here");
What I want is to have some text into the setFormat(); and an integer t follow.
Specifically, I need
Code:
ui->progressbar->setFormat("Starting with " + ui->listwidget->count()+ " files");
I get error
Code:
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.
Re: Simple Question about how to put an int to a function that requires coonst QStrin
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:
Code:
ui
->progressbar
->setFormat
(QString("Starting with %1 files").
arg(ui
->listwidget
->count
()) );
Re: Simple Question about how to put an int to a function that requires coonst QStrin
Quote:
Originally Posted by
hakermania
QProgressbar Format can be set as follow:
Code:
progressbar->setFormat("text here");
What I want is to have some text into the setFormat(); and an integer t follow.
Specifically, I need
Code:
ui->progressbar->setFormat("Starting with " + ui->listwidget->count()+ " files");
I get error
Code:
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.
Quote:
So, how will I do what I want?
.
You could make sure both "" strings are turned into QStrings before you try the +:
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
Code:
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.
Re: Simple Question about how to put an int to a function that requires coonst QStrin
Quote:
Originally Posted by
hakermania
I get error
Code:
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.
Re: Simple Question about how to put an int to a function that requires coonst QStrin
Thx for your replies! :):rolleyes:
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:
Code:
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-so...anslation.html