10

I just started learning C++ in Qt and I was wondering how can I put a variables result in a string? I'm trying to use this for a simple application where someone puts their name in a text field then presses a button and it displays there name in a sentence. I know in objective-c it would be like,

NSString *name = [NSString stringWithFormatting:@"Hello, %@", [nameField stringValue]];
[nameField setStringValue:name];

How would I go about doing something like this with C++? Thanks for the help

2
  • BTW...if your project is really committed to Qt, there are several good reasons to use the QString class instead of std::string...one of which is "implicit sharing". For some perspective: stackoverflow.com/questions/1618798/… Commented Sep 25, 2011 at 4:37
  • "Implicit sharing" is nice when you have limited memory and one CPU core, but slows down multi-threaded programs. Commented Sep 26, 2011 at 8:18

4 Answers 4

10

You don´t mention what type your string is. If you are using the standard library then it would be something along the lines of

std::string name = "Hello, " + nameField;

That works for concatenating strings, if you want to insert other complex types you can use a stringstream like this:

std::ostringstream stream;
stream << "Hello, " << nameField;
stream << ", here is an int " << 7;

std::string text = stream.str();

Qt probably has its own string types, which should work in a similar fashion.

Sign up to request clarification or add additional context in comments.

5 Comments

There we go! Thanks for the help, I'm supprised its that simple.
@Natatos: That's not very simple, in my mind, because you need to introduce another variable for the ostringstream to create your string.
@KenBloom: Well, you can always do it with a temporary, like ( std::ostringstream() << "Hello, " << nameField << ", here is an int " << 7 ).str(); but I try to avoid it if possible.
@K-ballo It is not that easy because as soon as you use <<, the stream is upcasted to std::ostream & which has no str() function, it might work if you are already using C++0x, with an updated STL, but for now, you have to static_cast it explicitly to ostringstream before calling str.
See this post for the correct code: stackoverflow.com/questions/303562/…
9

I assume we're talking about Qt's QString class here. In this case, you can use the arg method:

 int     i;           // current file's number
 long    total;       // number of files to process
 QString fileName;    // current file's name

 QString status = QString("Processing file %1 of %2: %3")
                 .arg(i).arg(total).arg(fileName);

See the QString documentation for more details about the many overloads of the arg method.

3 Comments

I suggest a single multi-arg arg(): ...").arg( i, total, fileName ). More robust, in case i or total happen to contain e.g. "%1". And probably also faster.
@FrankOsterfeld: There isn't an overload to arg() that can handle both ints and QStrings.
Ken: Right, you have to wrap the int in QString::number() then.
1

I would use a stringstream but I'm not 100% sure how that fits into your NSString case...

stringstream ss (stringstream::in);
ss << "hello my name is " << nameField;

I think QString has some nifty helpers that might do the same thing...

QString hello("hello ");
QString message =  hello % nameField;

2 Comments

QString has on operator% defined AFAICT (see doc.qt.nokia.com/latest/qstring.html)
See section called More Efficient String Construction in my link
0

You could use QString::sprintf. I haven't found a good example of it's use yet, though. (If someone else finds one, feel free to edit it in to this answer).

You might be interested in seeing information about the difference between QString::sprintf and QString::arg.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.