9

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Natatos
  • 135
  • 1
  • 2
  • 5
  • 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: http://stackoverflow.com/questions/1618798/why-is-there-a-different-string-class-in-every-c-platform-out-there – HostileFork says dont trust SE Sep 25 '11 at 04:37
  • "Implicit sharing" is nice when you have limited memory and one CPU core, but slows down multi-threaded programs. – MSalters Sep 26 '11 at 08:18

4 Answers4

8

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
7

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.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • There we go! Thanks for the help, I'm supprised its that simple. – Natatos Sep 25 '11 at 02:08
  • @Natatos: That's not very simple, in my mind, because you need to introduce another variable for the `ostringstream` to create your string. – Ken Bloom Sep 25 '11 at 02:10
  • @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 Sep 25 '11 at 02:12
  • 1
    @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. – alexisdm Oct 10 '11 at 10:11
  • See this post for the correct code: http://stackoverflow.com/questions/303562/c-format-macro-inline-ostringstream – parsley72 Aug 29 '12 at 03:24
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;
Andrew White
  • 52,720
  • 19
  • 113
  • 137
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.

Community
  • 1
  • 1
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168