A
A
AflameGhost2021-10-05 10:59:55
Qt
AflameGhost, 2021-10-05 10:59:55

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.

mainwindow.h
#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

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv) ;
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp
#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

1 answer(s)
S
ShakeR, 2021-10-05
@ShakeR

The file is created, but the name is read before it is entered into LineEdit

Reading occurs, only it comes from the form field of the MyClass class (which is not displayed, not filled and, accordingly, empty), and you enter the value in the form field of the MainWindow class.
It's easiest to do this:
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();
    }
};

and in mainwindow.cpp connect the signal to the slot like this:
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 question

Ask a Question

731 491 924 answers to any question