Answer the question
In order to leave comments, you need to log in
Ui doesn't work in new QT class. What to do?
It was necessary to create an additional class (besides MainWindow) that would create a file with the name entered through LineEdit after pressing pushButton. The file is created, but the name is read before it is entered into LineEdit (accordingly, an unnamed .txt file is created).
I tried a lot of things: inheriting MyClass from MainWindow, calling the read method from MainWindow, trying to implement input through signals and slots-> all without success.
There are thoughts that the problem is in the null pointer Ui::MainWindow *ui, also tried to fix it, but without success.
#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();
}
Answer the question
In order to leave comments, you need to log in
The file is created, but the name is read before it is entered into 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());
});
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question