Create a text file with a template

  • Hi,
    i have to generate a text file. For that i want to use a template.
    My first idea is to create a template which looks like:


    Filename: %1
    Author: %2
    Timestamp: %3 

    So when creating my file i can read that template and replace the %- values with the arg function of the QString class:

    QFile file("template.txt");;
    QTextStream in(&file);
    QString text = in.readAll();
    QString newText = text.arg("MyFilename").arg("Homer Simpson").arg("Today");
    // save newText in a new file

    In my template file are a lot of values to set, so the code is really ugly and hard to read with the number of .arg().arg().arg() ....

    Is there a better to realize that?


  • Lifetime Qt Champion


    The Grantlee project might be of interest for your use case.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.