Send int value between forms. From MainWindow to SecondDialog.



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



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


  • Hm okej, still not working..

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



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



  • 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 !!



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



  • 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
    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 :)



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

    }



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.