Z
Z
Zdaart55332020-04-08 20:34:58
Qt
Zdaart5533, 2020-04-08 20:34:58

Access to the form through the slot, how?

When you try to access widgets on the form through the slot, nothing happens. Perhaps this is how it should be?

form.cpp
#include "form.h"
        #include "ui_form.h"
        #include<mainwindow.h>
        Form::Form(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Form)
        {
            ui->setupUi(this);
            MainWindow* m = new MainWindow;
            connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(send()));
            connect(this, SIGNAL(sendData(QString)) ,m, SLOT(textchanged(QString)) );
        }

        Form::~Form()
        {
            delete ui;
        }
        void Form::send(){
            emit sendData(ui->lineEdit->text());
        }

mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<form.h>
#include<ui_form.h>.
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   // Form* form = new Form;
    connect(ui->pushButton , SIGNAL(clicked()) , this, SLOT(showadd()));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::textchanged(QString data){
    ui->lineEdit->setText(data);
}
void MainWindow::showadd(){
     Form* form = new Form;
     form->show();
}

form.h
#ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();
public slots:
    void send();
signals:
    void sendData(QString data);
private:
    Ui::Form *ui;
};

#endif // FORM_H

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
    void textchanged(QString data);
    void showadd();
private:
    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();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kastuś, 2020-04-12
@Gytim

At least what I see
1. The MainWindow form is created and deleted immediately in the constructor It is
necessary to declare the class in form.h
...
private:
Ui::Form *ui;
MainWindow *m;
};
initialize before connect in form.cpp
...
{
ui->setupUi(this);
m = new MainWindow;
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(send()));
connect(this, SIGNAL(sendData(QString)) ,m, SLOT(textchanged(QString)) );
...
2. After the first paragraph, you can remove the extra connection and directly call the method
in form.h
...
{
ui->setupUi(this);
m = new MainWindow;
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(send()));
connect(this, SIGNAL(sendData(QString)) ,m, SLOT(textchanged(QString)) );
...
void Form::send(){
emit sendData(ui->lineEdit->text());
m->textchanged(ui->lineEdit->text());
}
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question