Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    template.txt:

    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");
    file.open(QIODevice::ReadOnly);
    
    QTextStream in(&file);
    
    QString text = in.readAll();
    
    QString newText = text.arg("MyFilename").arg("Homer Simpson").arg("Today");
    
    // save newText in a new file
    
    file.close();
    

    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?

    Thanks!


  • Lifetime Qt Champion

    Hi,

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


Log in to reply