Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] How to save information to text file.

[SOLVED] How to save information to text file.

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 930 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paawan
    wrote on last edited by
    #1

    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){

    setName(name);
    setDate(rdate);
    setRecommend(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 (!mFile.open(QFile::WriteOnly | QFile::Text))
    
    {
        qDebug()<< "Cannot open File";
        return;
    }
    QTextStream out(&mFile);
    
    out<< mySoftware->getName();
    out<< " ";
    out << mySoftware->getRecommend();
    out<< " ";
    out <<mySoftware->getDate().toString();
    
    mFile.flush();
    mFile.close();
    

    @

    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

    Paawan

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Look again at the "OpenMode":http://qt-project.org/doc/qt-5/qiodevice.html#OpenModeFlag-enum it's just there.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Paawan
        wrote on last edited by
        #3

        Thanks a lot. I am sorted.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved