Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile and/or QTextStream not writing to correct file path.
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.4k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    achmed
    wrote on last edited by
    #1

    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();
    }
    @

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @
      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        achmed
        wrote on last edited by
        #3

        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.

        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