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. Send int value between forms. From MainWindow to SecondDialog.
QtWS25 Last Chance

Send int value between forms. From MainWindow to SecondDialog.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.2k 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.
  • J Offline
    J Offline
    Juno
    wrote on last edited by
    #1

    So this might be a rookie question, but right now I need all the help I can get.
    I'm doing a program where I have a MainWindow with Logg in functions.
    And after successfull logg in, I need to get the logged in persons id number from my MainWindow to the second dialog(LoggedInAsTeacher).

    But I dont really get the hang of Signals and Slots.
    How do I write the connect statement? And were? In both MainWindow and LoggedInAsTeacher? I only need to send it oneway, from MainWindow to LoggedInAsTeacher.
    This is what I got so far:

    mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QObject>
    #include "logginregister.h"
    #include "loggedinasteacher.h"

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int idNr() {return m_idNr;}

    private slots:
    void on_pushButton_loggIn_clicked();

    public slots:
    void setIdNr(int idNr);

    signals:
    void idNrChanged(int newIdNr);

    private:
    Ui::MainWindow *ui;
    LogginRegister lr;
    LoggedInAsTeacher *loggedTeacher;
    int m_idNr;

    };
    #endif // MAINWINDOW_H

    mainwindow.cpp
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QObject>
    #include "logginregister.h"
    #include "loggedinasteacher.h"

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int idNr() {return m_idNr;}

    private slots:
    void on_pushButton_loggIn_clicked();

    public slots:
    void setIdNr(int idNr);

    signals:
    void idNrChanged(int newIdNr);

    private:
    Ui::MainWindow *ui;
    LogginRegister lr;
    LoggedInAsTeacher *loggedTeacher;
    int m_idNr;
    };
    #endif // MAINWINDOW_H

    loggedinasteacher.h
    #ifndef LOGGEDINASTEACHER_H
    #define LOGGEDINASTEACHER_H
    #include "logginregister.h"
    #include <QDialog>

    namespace Ui {
    class LoggedInAsTeacher;
    }

    class LoggedInAsTeacher : public QDialog
    {
    Q_OBJECT

    public:
    explicit LoggedInAsTeacher(LogginRegister *lr, QWidget *parent = 0);
    ~LoggedInAsTeacher();
    int idNr() {return t_idNr;}

    public slots:
    void setIdNr(int idNr);

    signals:
    void idNrChanged(int newIdNr);

    private:
    Ui::LoggedInAsTeacher *ui;
    LogginRegister *modify;
    int t_idNr;

    };

    #endif // LOGGEDINASTEACHER_H

    loggedinasteacher.cpp
    #include "loggedinasteacher.h"
    #include "ui_loggedinasteacher.h"

    LoggedInAsTeacher::LoggedInAsTeacher(LogginRegister *lr, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoggedInAsTeacher)
    {
    ui->setupUi(this);
    this->modify = lr;
    ui->label_idNrInfo->setText(QString::fromStdString(lr->TeacherDisplayIdNr(t_idNr)));
    ui->label_nameInfo->setText(QString::fromStdString(lr->TeacherDisplayName(t_idNr)));
    ui->label_userNameInfo->setText(QString::fromStdString(lr->TeacherDisplayUserName(t_idNr)));
    }

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

    void LoggedInAsTeacher::setIdNr(int idNr)
    {
    t_idNr = idNr;
    emit idNrChanged(idNr);
    }

    FlotisableF 1 Reply Last reply
    0
    • J Juno

      So this might be a rookie question, but right now I need all the help I can get.
      I'm doing a program where I have a MainWindow with Logg in functions.
      And after successfull logg in, I need to get the logged in persons id number from my MainWindow to the second dialog(LoggedInAsTeacher).

      But I dont really get the hang of Signals and Slots.
      How do I write the connect statement? And were? In both MainWindow and LoggedInAsTeacher? I only need to send it oneway, from MainWindow to LoggedInAsTeacher.
      This is what I got so far:

      mainwindow.h
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QMainWindow>
      #include <QObject>
      #include "logginregister.h"
      #include "loggedinasteacher.h"

      #include <QMainWindow>

      namespace Ui {
      class MainWindow;
      }

      class MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
      int idNr() {return m_idNr;}

      private slots:
      void on_pushButton_loggIn_clicked();

      public slots:
      void setIdNr(int idNr);

      signals:
      void idNrChanged(int newIdNr);

      private:
      Ui::MainWindow *ui;
      LogginRegister lr;
      LoggedInAsTeacher *loggedTeacher;
      int m_idNr;

      };
      #endif // MAINWINDOW_H

      mainwindow.cpp
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QMainWindow>
      #include <QObject>
      #include "logginregister.h"
      #include "loggedinasteacher.h"

      #include <QMainWindow>

      namespace Ui {
      class MainWindow;
      }

      class MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
      int idNr() {return m_idNr;}

      private slots:
      void on_pushButton_loggIn_clicked();

      public slots:
      void setIdNr(int idNr);

      signals:
      void idNrChanged(int newIdNr);

      private:
      Ui::MainWindow *ui;
      LogginRegister lr;
      LoggedInAsTeacher *loggedTeacher;
      int m_idNr;
      };
      #endif // MAINWINDOW_H

      loggedinasteacher.h
      #ifndef LOGGEDINASTEACHER_H
      #define LOGGEDINASTEACHER_H
      #include "logginregister.h"
      #include <QDialog>

      namespace Ui {
      class LoggedInAsTeacher;
      }

      class LoggedInAsTeacher : public QDialog
      {
      Q_OBJECT

      public:
      explicit LoggedInAsTeacher(LogginRegister *lr, QWidget *parent = 0);
      ~LoggedInAsTeacher();
      int idNr() {return t_idNr;}

      public slots:
      void setIdNr(int idNr);

      signals:
      void idNrChanged(int newIdNr);

      private:
      Ui::LoggedInAsTeacher *ui;
      LogginRegister *modify;
      int t_idNr;

      };

      #endif // LOGGEDINASTEACHER_H

      loggedinasteacher.cpp
      #include "loggedinasteacher.h"
      #include "ui_loggedinasteacher.h"

      LoggedInAsTeacher::LoggedInAsTeacher(LogginRegister *lr, QWidget *parent) :
      QDialog(parent),
      ui(new Ui::LoggedInAsTeacher)
      {
      ui->setupUi(this);
      this->modify = lr;
      ui->label_idNrInfo->setText(QString::fromStdString(lr->TeacherDisplayIdNr(t_idNr)));
      ui->label_nameInfo->setText(QString::fromStdString(lr->TeacherDisplayName(t_idNr)));
      ui->label_userNameInfo->setText(QString::fromStdString(lr->TeacherDisplayUserName(t_idNr)));
      }

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

      void LoggedInAsTeacher::setIdNr(int idNr)
      {
      t_idNr = idNr;
      emit idNrChanged(idNr);
      }

      FlotisableF Offline
      FlotisableF Offline
      Flotisable
      wrote on last edited by
      #2

      @Juno
      since you have all the component in MainWindow, it is convenient to write the connect statement in the constructor of MainWindow

      for the statement of connect, you can read signals and slots document
      one way is written like this in your MainWindow's constructor

        connect( this, &MainWindow::idNrChanged, loggedTeacher, &LoggedInAsTeacher::setIdNr );
      
      1 Reply Last reply
      1
      • J Offline
        J Offline
        Juno
        wrote on last edited by
        #3

        Hm okej, still not working..

        Does all other code except the connect statement looks like it should work ?

        FlotisableF 1 Reply Last reply
        0
        • J Juno

          Hm okej, still not working..

          Does all other code except the connect statement looks like it should work ?

          FlotisableF Offline
          FlotisableF Offline
          Flotisable
          wrote on last edited by
          #4

          @Juno
          what's your code now?
          do you emit the idNrChanged signal?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Juno
            wrote on last edited by
            #5

            So this is what it looks like now:
            mainwindow.h
            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H

            #include <QMainWindow>
            #include <QObject>
            #include "logginregister.h"
            #include "loggedinasteacher.h"

            namespace Ui {
            class MainWindow;
            }

            class MainWindow : public QMainWindow
            {
            Q_OBJECT

            public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
            int idNr() const {return m_idNr; }

            public slots:
            void setIdNr(int idNr);

            signals:
            void idNrChanged(int newIdNr);

            private slots:
            void on_pushButton_loggIn_clicked();

            private:
            Ui::MainWindow *ui;
            LogginRegister lr;
            LoggedInAsTeacher *loggedTeacher;
            int m_idNr;
            };
            #endif // MAINWINDOW_H

            mainwindow.cpp
            #include "mainwindow.h"
            #include "ui_mainwindow.h"

            #include <QString>
            #include <QMessageBox>
            #include "loggedinasteacher.h"

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);
            connect( this, &MainWindow::idNrChanged, loggedTeacher, &LoggedInAsTeacher::setIdNr );
            lr.AddTeacher("Prof. Snape", "PrSn", "123456", "Teacher", 20000);
            this->loggedTeacher = new LoggedInAsTeacher(&this->lr, this);
            }

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

            void MainWindow::setIdNr(int idNr)
            {
            if(idNr != this->m_idNr)
            {
            this->m_idNr = idNr;
            emit idNrChanged(idNr);
            }
            }

            void MainWindow::on_pushButton_loggIn_clicked()
            {
            std::string username = ui->lineEdit_userName->text().toStdString();
            std::string password = ui->lineEdit_passWord->text().toStdString();

            bool correctLoggIn = false;
            correctLoggIn = lr.loggInCheck(username, password);
            
            if(correctLoggIn != false)
            {
                m_idNr = lr.GetTeacherId(username, password);
                this->hide();
                loggedTeacher->show();
            }
            else
            {
                 QMessageBox::warning(this, "Login", "Username or password is incorrect");
            }
            

            }

            loggedinasteacher.h
            #ifndef LOGGEDINASTEACHER_H
            #define LOGGEDINASTEACHER_H
            #include "logginregister.h"

            #include <QDialog>

            namespace Ui {
            class LoggedInAsTeacher;
            }

            class LoggedInAsTeacher : public QDialog
            {
            Q_OBJECT

            public:
            explicit LoggedInAsTeacher(LogginRegister *lr, QWidget *parent = 0);
            ~LoggedInAsTeacher();
            int idNr() const {return t_idNr; }

            public slots:
            void setIdNr(int idNr);

            signals:
            void idNrChanged(int newIdNr);

            private:
            Ui::LoggedInAsTeacher *ui;
            LogginRegister *modify;
            int t_idNr;
            };

            #endif // LOGGEDINASTEACHER_H

            loggedinasteacher.cpp
            #include "loggedinasteacher.h"
            #include "ui_loggedinasteacher.h"

            LoggedInAsTeacher::LoggedInAsTeacher(LogginRegister *lr, QWidget *parent) :
            QDialog(parent),
            ui(new Ui::LoggedInAsTeacher)
            {
            ui->setupUi(this);
            this->modify = lr;
            ui->label_idNrInfo->setText(QString::fromStdString(lr->TeacherDisplayIdNr(t_idNr)));
            ui->label_nameInfo->setText(QString::fromStdString(lr->TeacherDisplayName(t_idNr)));
            ui->label_userNameInfo->setText(QString::fromStdString(lr->TeacherDisplayUserName(t_idNr)));
            }

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

            void LoggedInAsTeacher::setIdNr(int idNr)
            {
            if(idNr != this->t_idNr)
            {
            this->t_idNr = idNr;
            emit idNrChanged(idNr);
            }
            }

            Where do you mean I should emit idNrChanged?
            Thank you so much for helping med !!

            FlotisableF 1 Reply Last reply
            0
            • J Juno

              So this is what it looks like now:
              mainwindow.h
              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H

              #include <QMainWindow>
              #include <QObject>
              #include "logginregister.h"
              #include "loggedinasteacher.h"

              namespace Ui {
              class MainWindow;
              }

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
              int idNr() const {return m_idNr; }

              public slots:
              void setIdNr(int idNr);

              signals:
              void idNrChanged(int newIdNr);

              private slots:
              void on_pushButton_loggIn_clicked();

              private:
              Ui::MainWindow *ui;
              LogginRegister lr;
              LoggedInAsTeacher *loggedTeacher;
              int m_idNr;
              };
              #endif // MAINWINDOW_H

              mainwindow.cpp
              #include "mainwindow.h"
              #include "ui_mainwindow.h"

              #include <QString>
              #include <QMessageBox>
              #include "loggedinasteacher.h"

              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
              {
              ui->setupUi(this);
              connect( this, &MainWindow::idNrChanged, loggedTeacher, &LoggedInAsTeacher::setIdNr );
              lr.AddTeacher("Prof. Snape", "PrSn", "123456", "Teacher", 20000);
              this->loggedTeacher = new LoggedInAsTeacher(&this->lr, this);
              }

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

              void MainWindow::setIdNr(int idNr)
              {
              if(idNr != this->m_idNr)
              {
              this->m_idNr = idNr;
              emit idNrChanged(idNr);
              }
              }

              void MainWindow::on_pushButton_loggIn_clicked()
              {
              std::string username = ui->lineEdit_userName->text().toStdString();
              std::string password = ui->lineEdit_passWord->text().toStdString();

              bool correctLoggIn = false;
              correctLoggIn = lr.loggInCheck(username, password);
              
              if(correctLoggIn != false)
              {
                  m_idNr = lr.GetTeacherId(username, password);
                  this->hide();
                  loggedTeacher->show();
              }
              else
              {
                   QMessageBox::warning(this, "Login", "Username or password is incorrect");
              }
              

              }

              loggedinasteacher.h
              #ifndef LOGGEDINASTEACHER_H
              #define LOGGEDINASTEACHER_H
              #include "logginregister.h"

              #include <QDialog>

              namespace Ui {
              class LoggedInAsTeacher;
              }

              class LoggedInAsTeacher : public QDialog
              {
              Q_OBJECT

              public:
              explicit LoggedInAsTeacher(LogginRegister *lr, QWidget *parent = 0);
              ~LoggedInAsTeacher();
              int idNr() const {return t_idNr; }

              public slots:
              void setIdNr(int idNr);

              signals:
              void idNrChanged(int newIdNr);

              private:
              Ui::LoggedInAsTeacher *ui;
              LogginRegister *modify;
              int t_idNr;
              };

              #endif // LOGGEDINASTEACHER_H

              loggedinasteacher.cpp
              #include "loggedinasteacher.h"
              #include "ui_loggedinasteacher.h"

              LoggedInAsTeacher::LoggedInAsTeacher(LogginRegister *lr, QWidget *parent) :
              QDialog(parent),
              ui(new Ui::LoggedInAsTeacher)
              {
              ui->setupUi(this);
              this->modify = lr;
              ui->label_idNrInfo->setText(QString::fromStdString(lr->TeacherDisplayIdNr(t_idNr)));
              ui->label_nameInfo->setText(QString::fromStdString(lr->TeacherDisplayName(t_idNr)));
              ui->label_userNameInfo->setText(QString::fromStdString(lr->TeacherDisplayUserName(t_idNr)));
              }

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

              void LoggedInAsTeacher::setIdNr(int idNr)
              {
              if(idNr != this->t_idNr)
              {
              this->t_idNr = idNr;
              emit idNrChanged(idNr);
              }
              }

              Where do you mean I should emit idNrChanged?
              Thank you so much for helping med !!

              FlotisableF Offline
              FlotisableF Offline
              Flotisable
              wrote on last edited by
              #6

              @Juno
              I'm not sure if I misunderstand what you want to do

              when logging in, MainWindow should send the id through the signal idNrChanged and LoggedInAsTeacher should get the id from the slot setId, right?

              if this is what you want, then when you set the id in MainWindow, it should emit the idNrChanged signal.
              after read your code, it seems you set the id by clicking the pushbutton, and in that slot, you set the id but do not emit the idNrChanged signal.

              so I think that may be the reason why it does not work.

              J 1 Reply Last reply
              0
              • Chris HennesC Offline
                Chris HennesC Offline
                Chris Hennes
                wrote on last edited by Chris Hennes
                #7

                Is it really necessary to use a signal for this? You've got a pointer to your LoggedInAsTeacher dialog in MainWindow -- why not just write a function in LoggedInAsTeacher called setID(int ID) and call it from your on_pushButton_loggIn_clicked() function?

                Chris Hennes, Pioneer Library System

                J 1 Reply Last reply
                0
                • Chris HennesC Chris Hennes

                  Is it really necessary to use a signal for this? You've got a pointer to your LoggedInAsTeacher dialog in MainWindow -- why not just write a function in LoggedInAsTeacher called setID(int ID) and call it from your on_pushButton_loggIn_clicked() function?

                  J Offline
                  J Offline
                  Juno
                  wrote on last edited by
                  #8

                  @Chris-Hennes
                  I have tried this. But it dosent work :/

                  void MainWindow::on_pushButton_loggIn_clicked()
                  {
                  std::string username = ui->lineEdit_userName->text().toStdString();
                  std::string password = ui->lineEdit_passWord->text().toStdString();

                  bool correctLoggIn = false;
                  correctLoggIn = lr.loggInCheck(username, password);
                  
                  if(correctLoggIn != false)
                  {
                      m_idNr = lr.GetTeacherId(username, password);
                      this->hide();
                      loggedTeacher->setIdNr(m_idNr); //<<<<<<
                      loggedTeacher->show();
                  }
                  else
                  {
                      QMessageBox::warning(this, "Login", "Username or password is incorrect");
                  }
                  

                  }

                  This is in my constructor for LoggedInAsTeacher. In loggedinasteacher.cpp
                  LoggedInAsTeacher::LoggedInAsTeacher(LogginRegister *lr, QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::LoggedInAsTeacher)
                  {
                  ui->setupUi(this);
                  this->modify = lr;

                  ui->label_idNrInfo->setText(QString::fromStdString(lr->TeacherDisplayIdNr(t_idNr)));
                  ui->label_nameInfo->setText(QString::fromStdString(lr->TeacherDisplayName(t_idNr)));
                  ui->label_userNameInfo->setText(QString::fromStdString(lr->TeacherDisplayUserName(t_idNr)));
                  

                  }

                  Thank you :)

                  1 Reply Last reply
                  0
                  • FlotisableF Flotisable

                    @Juno
                    I'm not sure if I misunderstand what you want to do

                    when logging in, MainWindow should send the id through the signal idNrChanged and LoggedInAsTeacher should get the id from the slot setId, right?

                    if this is what you want, then when you set the id in MainWindow, it should emit the idNrChanged signal.
                    after read your code, it seems you set the id by clicking the pushbutton, and in that slot, you set the id but do not emit the idNrChanged signal.

                    so I think that may be the reason why it does not work.

                    J Offline
                    J Offline
                    Juno
                    wrote on last edited by
                    #9

                    @Flotisable

                    void MainWindow::on_pushButton_loggIn_clicked()
                    {
                    std::string username = ui->lineEdit_userName->text().toStdString();
                    std::string password = ui->lineEdit_passWord->text().toStdString();

                    bool correctLoggIn = false;
                    correctLoggIn = lr.loggInCheck(username, password);
                    
                    if(correctLoggIn != false)
                    {
                        m_idNr = lr.GetTeacherId(username, password);
                        emit idNrChanged(m_idNr); // <--- Something like this ?
                        this->hide();
                        loggedTeacher->show();
                    }
                    else
                    {
                         QMessageBox::warning(this, "Login", "Username or password is incorrect");
                    }
                    

                    }

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Juno
                      wrote on last edited by
                      #10

                      So I thank both of you for your help. My problem is solved. :)
                      And it was not necessary to use signals and slots :)

                      1 Reply Last reply
                      0

                      • Login

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