mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow> // lines 5-14 for input
#include <QDialog>
#include <QTextEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFile>
#include <QIODevice>
#include <QTextStream>
class MyClass:public QDialog // class for writting
{
Q_OBJECT
public:
QPushButton *SaveButton;
QHBoxLayout *Hlay;
QTextEdit *textEdit;
explicit MyClass(QWidget *parent = 0);
private slots:
void writeToFile();
};
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MyClass::writeToFile()
{
QString fname = "C:\Users\morta\QT Projects\savename\out.txt";
QFile file(fname);
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream stream(&file);
}
file.close();
}
MyClass::MyClass(QWidget *parent):QDialog(parent)
{
setWindowTitle("Title");
SaveButton = new QPushButton;
textEdit = new QTextEdit;
SaveButton->setGeometry(50,100,150,80);
Hlay = new QHBoxLayout;
Hlay->addWidget(textEdit);
SaveButton->setText("Save");
Hlay->addWidget(SaveButton);
setLayout(Hlay);
QObject::connect(SaveButton, SIGNAL(clicked()), this, SLOT(writeToFile()));
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
My results
Doing my Electric Engineer Senior project, I was given a Gui assignment, at the moment im stuck here. I need to store names in a .txt file and later on have them in a list format. This file is me practicing before I go to my main project. For some odd reason nothing saves in my out.txt even after closing Qt. I tried changing the directory where it should save but no luck.
New contributor
Ruben P is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.