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

Write changing Object Values to new created File.



  • Hey,
    i'm pretty new to QT and also don't have to much knowledge about C++.
    I have a simple Sequential Animation.
    What i am trying to do is that my fileio.cpp writes all the changing values of my rect1 class down into an empty file. Pls help my find my problem.

    main.qml

    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2
    import QtQuick.Dialogs 1.2

    ApplicationWindow {
    title: qsTr("Hello World")
    width: 800
    height: 800
    visible: true
    id: window1

    /*function globals() {
    if (rect1.x >= 1) {
        FileIO.save(rect1.x)
        }
    }*/
    
    Rectangle {
        width: window1.width/6
        height: window1.width/6
        visible: true
        color: "green"
        id: rect1
        onXChanged: FileIO.save(rect1.x, rect1.y)
        onYChanged: FileIO.save(rect1.x, rect1.y)
    
    }
    
        SequentialAnimation {
            running: true
            loops: 1
            NumberAnimation { target: rect1; property: "x"; to: 200; duration: 1000 }
            NumberAnimation { target: rect1; property: "y"; to: 200; duration: 1000 }
            NumberAnimation { target: rect1; property: "x"; to: 300; duration: 1000 }
            NumberAnimation { target: rect1; property: "y"; to: 300; duration: 1000 }
            NumberAnimation { target: rect1; property: "x"; to: 50; duration: 1000 }
            NumberAnimation { target: rect1; property: "y"; to: 240; duration: 1000 }
        }
    
    Button {
        id: button1
        x: 285
        y: 265
        text: qsTr("Save x-Position")
    
        }
    

    }

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "fileio.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    engine.rootContext()->setContextProperty("FileIO",  new FileIO());
    
    return app.exec();
    

    }

    fileio.h

    #include <QObject>

    #ifndef FILEIO_H
    #define FILEIO_H
    #include <QList>

    class FileIO : public QObject
    {

    Q_OBJECT
    

    public:
    FileIO();
    //Q_INVOKABLE void save(QString text);
    Q_INVOKABLE void save(QString intsizeX, QString intsizeY);
    ~FileIO();

    private:
    //QList<std::map<int, int>> DataList;

    };

    #endif // FILEIO_H

    fileio.cpp

    #include "fileio.h"
    #include <QFile>
    #include <QTextStream>
    #include <QDebug>

    FileIO::FileIO()
    {

    }

    void FileIO::save(QString intsizeX, QString intsizeY) {

    QFile file("text.txt");
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
    QTextStream out(&file);
    out << intsizeX << intsizeY << endl;
    
    file.close();
    }
    
    //DataList.append(new std::map(intsizeX, intsizeY));
    
    return;
    

    }

    FileIO::~FileIO()
    {

    }

    I suggest the problem lies in the fileio.cpp. Everytime it saves the values it creates a new text.txt but i do not know how to solve it or if it is the real problem.
    Thx Don.


Log in to reply