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. Write changing Object Values to new created File.
QtWS25 Last Chance

Write changing Object Values to new created File.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 212 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.
  • D Offline
    D Offline
    DonMugg
    wrote on last edited by
    #1

    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.

    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