Help me fix the program with the signals. Qt 5.10

Good evening.
There was a problem with the signals.
You need to pass the data from the Form to the MainWindow. Without an additional form, everything is implemented, but it is not clear how to connect the two together.

Please help me. I apologize in advance for the redneck code.

I can't correctly insert the code here, so the download link is: tyk

mainwindow

form

Mainwindow.h

public slots:
    void recieveData(QString str);

private slots:

    void on_ResultButton_clicked();

    void on_EnterDataButton_clicked();

Form.h

signals:
    void sendData(QString str);

private slots:
    void on_pushButton_clicked();
    void onButtonSend();

public:
    explicit Form(QWidget *parent = 0);
    ~Form();

Form.cpp

void Form::on_pushButton_clicked()
{
    connect(this, SIGNAL(sendData(QString)), mainwindow, 
SLOT(recieveData(QString)));
}

void Form::onButtonSend()
{
    QString str_DataEditForm (ui -> dateEditForm -> text());
    ui -> HBDay -> setText(str_DataEditForm);
    emit sendData(ui -> HBDay -> text());
    connect(ui -> pushButton, SIGNAL(clicked()), this, 
SLOT(onButtonSend()));
}

Mainwindow.cpp

void MainWindow::recieveData(QString str)
{
    connect(myform, SIGNAL(sendData(QString)), this, SLOT(recieveData(QString)));
    QString data = str;
    ui -> HBDay -> setText(data);
}

    void MainWindow::on_EnterDataButton_clicked()
{
    connect(ui->EnterDataButton, SIGNAL(clicked()), myform, SLOT(show()));
}
Author: Thomas MacCort, 2018-01-03

1 answers

Here is a working option: Download link ready source code

Form.h

#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include <QDate>

namespace Ui {
class Form;
}

class Form : public QWidget
{
Q_OBJECT

public:
explicit Form(QWidget *parent = 0);
~Form();

signals:
void sigSetDateTime(QDate);

private slots:
void slotSetDateTime();

private:
Ui::Form *ui;
};

#endif // FORM_H

Form.cpp

#include "form.h"
#include "ui_form.h"
#include <QPushButton>
#include <QDateEdit>
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"

Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked(bool)), this,     SLOT(slotSetDateTime()));
}

Form::~Form()
{
delete ui;
}

void Form::slotSetDateTime()
{
//-- Создали промежуточный слот для формирования сигнала со значением дата
qDebug() << "Signal from form";
emit sigSetDateTime(ui->dateEditForm->date());
}

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "form.h"
#include <QMainWindow>
#include <QStringList>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots: //определение функций и кнопок
void on_ResultButton_clicked();
    void on_EnterDataButton_clicked();

private:
Ui::MainWindow *ui;
Form *myform;
};

#endif // MAINWINDOW_H

Mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDateEdit>
#include "form.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myform = new Form();

    //-- соединяем сигнал из формы с полем "дата" в главном окне
    connect(myform, SIGNAL(sigSetDateTime(QDate)), ui->dateEdit, SLOT(setDate(QDate)));
}

void MainWindow::on_ResultButton_clicked()
{
qint64 y;

//Вывод текущей даты
QDate currDate = QDate::currentDate();
ui -> NowTimeLabel -> setText(currDate.toString("MM/dd/yyyy"));

//Вывод введенной даты
QString str_DataEdit (ui -> dateEdit -> text());
ui -> HBDay -> setText(str_DataEdit);
QDate d = ui -> dateEdit -> date();

//Сравнение
QDate entDate(d.year(), d.month(), d.day());
y = entDate.daysTo(currDate);

//Вывод разницы между датами в label
QString y1 = QString::number(y);
ui -> DiffLabel -> setText(y1);

//Вычисление данных и их вывод
int Prospal, Morgnul, Udarov, Perekachalo, Vipito, Smeyalsya;
Prospal = 8;
Prospal = Prospal * y;
QString Prospal1 = QString::number(Prospal);
ui -> ProspalCounter -> setText(Prospal1);

Morgnul = 230137;
Morgnul = Morgnul * y;
QString Morgnul1 = QString::number(Morgnul);
ui -> MorgnulCounter -> setText(Morgnul1);

Udarov = 70000;
Udarov = Udarov * y;
QString Udarov1 = QString::number(Udarov);
ui -> UdarovCounter -> setText(Udarov1);

Perekachalo = 2000;
Perekachalo = Perekachalo * y;
QString Perekachalo1 = QString::number(Perekachalo);
ui -> PerekachaloCounter -> setText(Perekachalo1);

Vipito = 2;
Vipito = Vipito * y;
QString Vipito1 = QString::number(Vipito);
ui -> VipitoCounter -> setText(Vipito1);

Smeyalsya = 20;
Smeyalsya = Smeyalsya * y;
QString Smeyalsya1 = QString::number(Smeyalsya);
ui -> SmeyalsyaCounter -> setText(Smeyalsya1);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_EnterDataButton_clicked()
{
myform->show(); // показываем форму
}
 0
Author: Compozitor, 2018-01-29 13:23:16