Skip to content

Instantly share code, notes, and snippets.

@ThereExistsX
Created February 26, 2015 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThereExistsX/cc44597d05e5e42c0b3d to your computer and use it in GitHub Desktop.
Save ThereExistsX/cc44597d05e5e42c0b3d to your computer and use it in GitHub Desktop.
File IO
#include "fileio.h"
#include <QFile>
#include <QTextStream>
FileIO::FileIO()
{
}
void FileIO::save(QString text){
QFile file("text.txt");
if(file.open(QIODevice::ReadWrite)){
QTextStream stream(&file);
stream << text << endl;
}
return;
}
FileIO::~FileIO()
{
}
#include <QObject>
#ifndef FILEIO_H
#define FILEIO_H
class FileIO : public QObject
{
Q_OBJECT
public:
FileIO();
Q_INVOKABLE void save(QString text);
~FileIO();
};
#endif // FILEIO_H
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "fileio.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.rootContext()->setContextProperty("FileIO", new FileIO());
return app.exec();
}
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: 640
height: 480
visible: true
TextArea {
id: saveMe
x: 200
y: 84
}
Button {
id: button1
x: 283
y: 266
text: qsTr("Save")
onClicked: FileIO.save(saveMe.text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment