Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QSignalMapper instead of Signals and slots
Forum Updated to NodeBB v4.3 + New Features

Using QSignalMapper instead of Signals and slots

Scheduled Pinned Locked Moved Solved General and Desktop
qt creatorqsignalmappersignal & slotqstring
4 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kushan
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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_OBJECT

    public:
    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_OBJECT

    private 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?

    jsulmJ 1 Reply Last reply
    0
    • K Kushan

      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_OBJECT

      public:
      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_OBJECT

      public:
      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_OBJECT

      private 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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      2
      • jsulmJ jsulm

        @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.

        K Offline
        K Offline
        Kushan
        wrote on last edited by
        #3

        @jsulm How can I set a public setter method in Dialog2 to receive the value from MainWindow?

        jsulmJ 1 Reply Last reply
        0
        • K Kushan

          @jsulm How can I set a public setter method in Dialog2 to receive the value from MainWindow?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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);
          }
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved