Question

I am trying to set the text that appears on a tab to something like this

~Untitled(n)

Where "n" is the index of the tab. I am having trouble concatenating the string and integer. This is what I have tried.

armaTab->addTab(new QWidget, "~Untitled (" + QString(armaTab->currentIndex() + 1) + ")");

With that, i end up getting something that looks like this:

~Untitled([])

What is the proper way to concatenate the string and integer to produce the desired result?

Was it helpful?

Solution 2

Try using QString::number(n). This will convert the integer to a QString which you can concatenate to your original string.

OTHER TIPS

"~Untitled (" + QString::number(armaTab->currentIndex() + 1) + ")"

= OR =

QString("~Untitled(%1)").arg(armaTab->currentIndex() + 1)

QString offers the arg function:

QString("~Untitled %1").arg(armaTab->currentIndex() + 1)

This question already have few good answers. I'm adding another alternate way to concatenate string and integer using stringstream

stringstream ss(str);
ss<<n;
string temp = ss.str();

or we can get the concatenated string by following logic

 string temp = str + to_string(n);

Now we can get the QString from string:

QString str = QString::fromUtf8(temp.c_str());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top