QFile and/or QTextStream not writing to correct file path.



  • In my main program I create two instances of LogClass called "Log1" and "Log2".
    I Set the file paths for both objects.
    Then write "Data1" to "Log1" and "Data2" to "Log2".
    After executing the program, only Log2.txt is created and it contains both "Data1" and "Data2".
    If I swop around the two lines of code that sets the file paths of Log1 and Log2.
    Then the opposite happens. Only Log1.txt is created and containes "Data1" and "Data2".

    Why are the Two files paths getting mixed up?

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "logclass.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    LogClass *Log1;
    LogClass *Log2;
    };

    #endif // MAINWINDOW_H
    @

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    Log1 = new LogClass();
    Log2 = new LogClass();

    Log1->SetFilePath("c:/Log1.txt");
    Log2->SetFilePath("c:/Log2.txt");
    
    Log1->WriteData("Data1");
    Log2->WriteData("Data2");
    

    }
    @

    @#ifndef LOGCLASS_H
    #define LOGCLASS_H

    #include <QObject>
    #include <QFile>
    #include <QTextStream>
    #include <QDateTime>

    class LogClass : public QObject
    {
    Q_OBJECT

    public:

    //QString LogStr;
    
    explicit LogClass(QObject *parent = 0);
    

    public slots:
    void SetFilePath(QString PATH);
    void WriteData(QString message);

    };

    #endif // LOGCLASS_H
    @

    @#include "logclass.h"

    QFile LogFile("c:/Log.csv");
    QTextStream LogStream(&LogFile);

    LogClass::LogClass(QObject *parent) :
    QObject(parent)
    {
    }

    void LogClass::SetFilePath(QString PATH)
    {
    LogFile.setFileName(PATH);
    }

    void LogClass::WriteData(QString message)
    {
    LogFile.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
    LogStream<<message;
    LogFile.close();
    }
    @


  • Moderators

    @
    QFile LogFile("c:/Log.csv");
    @

    Because you defined LogFile not as a member of the class, but as a "global variable":http://www.learncpp.com/cpp-tutorial/42-global-variables/ instead. So they get shared for all instances and the path is overwritten by the last instance which sets the path.
    Move it as a member to the class to behave it like you are expecting.



  • Thanks. I Thought it was only global to this particular instance of the class and not between all instances. I changed it to pointers and now declared them as private. Problem fixed. Thanks for your quick reply.


Log in to reply
 

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