Greetings to everyone,
I'm new to Qt programming and have one question on simple function.
I have mainWindows with lineEdit and pushButton. Clicked on push button open new theDialog. theDialog contain three lineEdit, which on first two entered number and third is sum of previous two number. On pushButton I want to write this result in lineEdit on mainWindow. There is no error, but I couldn't get result.
Here is code:
mainwindow.h
mainwindow.cpp#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void textValue(const QString& arg);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
thedialog.h#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "thedialog.h"
#include <QDebug>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
theDialog *thedialog = new theDialog;
QObject::connect(thedialog, SIGNAL(textEdited(QString)), this, SLOT(textValue(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:n_pushButton_clicked()
{
theDialog thedialog;
thedialog.setModal(true);
thedialog.exec();
}
void MainWindow::textValue(const QString &arg)
{
ui->lineEdit->setText(arg);
qDebug()<<"Message from textValue Function";
}
thedialog.cpp#ifndef THEDIALOG_H
#define THEDIALOG_H
#include <QDialog>
namespace Ui {
class theDialog;
}
class theDialog : public QDialog
{
Q_OBJECT
public:
explicit theDialog(QWidget *parent = 0);
~theDialog();
signals:
void textEdited(const QString& arg);
public slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
public:
Ui::theDialog *ui;
};
#endif // THEDIALOG_H
main.cpp#include "thedialog.h"
#include "ui_thedialog.h"
#include <QDebug>
theDialog::theDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::theDialog)
{
ui->setupUi(this);
}
theDialog::~theDialog()
{
delete ui;
}
void theDialog:n_pushButton_clicked()
{
double lineEdit = ui->lineEdit->text().toDouble();
double lineEdit_2 = ui->lineEdit_2->text().toDouble();
double result = lineEdit + lineEdit_2;
ui->lineEdit_3->setText(QString::number(result));
}
void theDialog:n_pushButton_2_clicked()
{
emit textEdited(ui->lineEdit_3->text());
qDebug()<<"Sent Message";
theDialog::close();
}
Thanks in advance.#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Bookmarks