Using QSignalMapper instead of Signals and slots
-
In my Qt c++ application I have 3 classes namely MainWindow.cpp, Dialog.cpp and Dialog2.cpp! Currently I am taking a variable value from a QlineEdit iin MainWindow.ui and sending it to Dialog2.cpp through Dialog.cpp using singal slot mechanism!
following is my code
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
QString value;
explicit MainWindow(QWidget *parent = 0);
~MainWindow();signals:
void sendValue(QString val);private slots:
void on_pushButton_clicked();private:
Ui::MainWindow *ui;
};MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
value=ui->line_Edit->text();
Dialog dialog1;
connect(this,SIGNAL(sendValue(QString)),&dialog1,SLOT(receiveValue(QString)));
emit sendValue(value);
}Dialog.h
#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
namespace Ui {
class Dialog;
}class Dialog : public QDialog
{
Q_OBJECTpublic:
QString value;
explicit Dialog(QWidget *parent = 0);
~Dialog();private slots:
void receiveValue(QString val)
void on_pushButton_clicked();signals:
void sendValue2(QString val);private:
Ui::Dialog *ui;
};#endif // DIALOG_H
Dialog.cpp
#include "dialog2.h"
#include "dialog.h"
#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}Dialog::~Dialog()
{
delete ui;
}void Dialog::receiveValue(QString val)
{
value=val;}
void Dialog::on_pushButton_clicked()
{
Dialog2 dialog2;
connect(this,SIGNAL(sendValue2(QString)),&dialog2,SLOT(receiveValue2(QString)));
emit sendValue2(value);}
Dailog2.h
#ifndef DIALOG2_H
#define DIALOG2_H#include <QDialog>
namespace Ui {
class Dialog2;
}class Dialog2 : public QDialog
{
Q_OBJECTprivate slots:
void receiveValue2 (QString val)public:
QString Value
explicit Dialog2(QWidget *parent = 0);
~Dialog2();private:
Ui::Dialog2 *ui;
};#endif // DIALOG2_H
Dialog2.cpp
#include "dialog2.h"
#include "ui_dialog2.h"Dialog2::Dialog2(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog2)
{
ui->setupUi(this);
}Dialog2::~Dialog2()
{
delete ui;
}void Dialog2::receiveValue2(QString val){
Value=val;}
How can I directly send from MianWindow.cpp to Dialog2.cpp using the QSignalMapper?
-
@Kushan For such a use case I don't see any need for QSignalMapper. Why do you want to use it?
"How can I directly send from MianWindow.cpp to Dialog2.cpp using the QSignalMapper?" - what do you mean by "directly". You are already sending from MainWindow to Dialog2. You could even skip the whole signal/slot here and pass the parameter either to Dialog2 constructor or add a public setter method to Dialog2 to set the value.
Signals/Slots are usually used for loose coupling: if sender and receiver do not need to know anything about each other. For example, a QPushButton does not care who is doing something if it is pressed: it just emits the signals and who ever wants to know about this signal connects a slot to it. -
@Kushan Simple C++:
class Dialog2 : public QDialog { Q_OBJECT private slots: void receiveValue2 (QString val) public: QString Value explicit Dialog2(QWidget *parent = 0); ~Dialog2(); void setValue(const QString &val) { Value=val; } private: Ui::Dialog2 *ui; }; ... void MainWindow::on_pushButton_clicked() { value=ui->line_Edit->text(); Dialog dialog1; dialog1.setValue(value); }