Important: Please read the Qt Code of Conduct -

[SOLVED] How to save information to text file.

  • Hi All,

    One of my assignment questions asks me to display a screen where a user enters software name, date of release and if it is recommended or not. Then store the information.

    I made a UI class derived from QDialog that display the fields.

    I then made a "Software" class that has the three variables.

    @ software::software(QString name, QDate rdate, QString recommend){



    I then made a software writer class which takes the "Software" class object and writes the info to a text file.

    SoftwareWriter::SoftwareWriter(software *mySoftware){

    QString mFileName = "F:/mySoftware.txt";
    QFile mFile(mFileName);
    if (! | QFile::Text))
        qDebug()<< "Cannot open File";
    QTextStream out(&mFile);
    out<< mySoftware->getName();
    out<< " ";
    out << mySoftware->getRecommend();
    out<< " ";
    out <<mySoftware->getDate().toString();


    I have added a "Add" button the the UI class. Which when clicked triggers

    void InputGUI::on_pbAdd_clicked()
    name = ui->leName->text();
    rdate = ui->deDate->date();
    recommend = "No";

    if (ui->cb->isChecked()){
        recommend = "Yes";
    software * mySoftware = new software(name, rdate, recommend);

    SoftwareWriter write(mySoftware);



    My problem is whenever I click on the "Add" button a new file is created instead of the same file being used to store the information.

    How do I fix that problem?

    Thanks and Regards


  • Lifetime Qt Champion


    Look again at the "OpenMode": it's just there.

  • Thanks a lot. I am sorted.

Log in to reply