#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
namespace Ui{
class MainWindow;
}
Class MyClass : public QMainWindow
{
Q_OBJECT
public:
Ui::MainWindow *ui = new Ui::MainWindow;
public slots:
void newSlot();
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MyClass q;
explicit MainWindow(QWidget *parent= 0);
~MainWindow();
Ui::MainWindow *ui;
};
#endif //MAINWINDOW_H
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv) ;
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <fstream>
#include <iostream>
#include <QFileInfo>
#include <QTextStream>
#include <QLineEdit>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui (new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), &q, SLOT(newSlot()));
};
MainWindow::~MainWindow()
{
delete ui;
}
void MyClass::newSlot()
{
ui->setupUi(this);
QString str=ui->lineEdit->text();
QFile file(str.toUtf8()+”.txt”);
if (file.open(QIODevice::WriteOnly)){
QString r=QFileInfo(“.txt”).absolutePath();
QTextStream out (&file);
out<<r.toUtf8()<<endl;}
file.close();
}
Файл создаётся, но считывание названия происходит до его введения в LineEdit
Class MyClass : public QMainWindow
{
Q_OBJECT
public:
Ui::MainWindow *ui = new Ui::MainWindow;
public slots:
void newSlot(QString fileName)
{
QFile file(fileName.toUtf8()+”.txt”);
if (file.open(QIODevice::WriteOnly)){
QString r=QFileInfo(fileName.toUtf8()+“.txt”).absolutePath();
QTextStream out (&file);
out<<r.toUtf8()<<endl;
}
file.close();
}
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui (new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, &QPushButton::clicked, this, [this](){
q.newSlot(ui->lineEdit->text());
});
};