PDA

View Full Version : QString arg



PLUS
29th January 2010, 10:42
I want to use QString to produce something like following:


This is number 99.

I should note that i want to be able to change it to the following if needed:

This_is_number_99.
or this one:

This-is-number-99.

in which spaces are dynamic, so I used this code:


QString test=" ";
QMessageBox::information(this,"Testing...",tr("This%1is%1number%199.").arg(test));

the problem is that, because %1 is followed by another number, it does not work as expected, please someone shows me a way to solve this,

any help is appreciated.

high_flyer
29th January 2010, 11:41
QMessageBox::information(this,"Testing...",tr(QString("This is number %1.").arg(99)));

But you can also do:

QMessageBox::information(this,"Testing...",tr(QString("This is number ")+QString::number(99)));

PLUS
29th January 2010, 13:25
Thanks for your reply, but I think you didn't understand what I mean,

I want SPACES to be variable and that number is static!!

please someone helps if there is a way...

problem is not the number, problem is %1 followed by a number!

faldzip
29th January 2010, 13:48
That's what high_flyer said applied to your code:


QString test=" ";
QMessageBox::information(this,"Testing...",tr("This%1is%1number%1%2.").arg(test).arg(99));

// or

QString test=" ";
QMessageBox::information(this,"Testing...",tr("This%1is%1number%1.").arg(test)+QString::number(99));

PLUS
29th January 2010, 13:57
That's what high_flyer said applied to your code:


QString test=" ";
QMessageBox::information(this,"Testing...",tr("This%1is%1number%1%2.").arg(test).arg(99));

// or

QString test=" ";
QMessageBox::information(this,"Testing...",tr("This%1is%1number%1.").arg(test)+QString::number(99));


Thanks for your help,

thats a nice way, but I was looking for a more convenience way, like specifying {%1} or %{1} or similar things.

so there is no such way,

again thanks for your reply.

faldzip
29th January 2010, 14:13
Another thing which came to my mind is that maybe you can use QString::split() and QStringList::join():


QString test("_");
QString result = QString("This is number 99.").split(' ').join(test);
// this should give result = "This_is_number_99"

franz
30th January 2010, 10:05
Why not just


QString txt = "This is text with spaces";
txt.replace(" ", "_");

faldzip
30th January 2010, 10:59
Why not just


QString txt = "This is text with spaces";
txt.replace(" ", "_");


You're right... -_-'
:D sometimes the simplest solution is the last one you are thinking of :]