[Qt-interest] QT string templating

Constantin Makshin cmakshin at gmail.com
Tue Feb 8 15:19:00 CET 2011


Although not very important in this particular case, in general I think it's better to replace numbers in the first place to make sure that "%..." parts that may be present in replacement strings will stay intact.

Example:

#include <QDebug>
#include <QString>
int main (int, char**)
{
    QLatin1String str("[%1]");
    int num = 123;
    qDebug() << QString("str, num: str=%1, num=%2").arg(str).arg(num);
    qDebug() << QString("num, str: str=%2, num=%1").arg(num).arg(str);
    return 0;
}

Outputs:
"str, num: str=[123], num=%2" 
"num, str: str=[%1], num=123"

On Tuesday 08 February 2011 16:51:59 Sean Harmer wrote:
> Hi,
> 
> On Tuesday 08 February 2011 19:14:45 Mandeep Sandhu wrote:
> > Is there any support for string templating in QT?
> > 
> > I came upon Grantlee (http://www.gitorious.org/grantlee/pages/Home),
> > but that seems to have a lot more features than my simple requirement.
> > 
> > For eg: if given a string "employee=<EMP_NAME>,
> > employee_num=<EMP_NUM>", I need a a way to specify that <EMP_NAME> and
> > <EMP_NUM> be replaced by certain values.
> 
> QList<Employee> employees = ...
> 
> foreach ( const Employee& employee, employees )
> {
> 	QString string = QString( "employee=%1, employee_num=%2" )
> 		.arg( employee.name )
> 		.arg( employee.number );
> 	qDebug() << string;
> }
> 
> Sean
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110208/1f60f8c4/attachment.bin 


More information about the Qt-interest-old mailing list