Domanda

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?

È stato utile?

Soluzione 2

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

Altri suggerimenti

"~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());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top