PDA

View Full Version : Sending int value between forms. From mainwindow to second dialog.



jUno
4th May 2017, 11:57
So, I'm completely stuck. Been searching and googeling for the last few days.

In my mainwindow, I have Loggin functions, and after pressing the "Loggin" button, and username and password is correct, I open a second dialog "loggedinasteacher".
When in this second dialog window I need the id-number corresponding to the username and password. This IdNr I need to send from my mainwindow to my second dialog.
And this is the problem, I can't pass it.

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"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

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);
lr.AddTeacher("Prof. Snape", "PrSn", "123456", "Teacher", 20000);
this->loggedTeacher = new LoggedInAsTeacher(&this->lr, this);
}

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

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();


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

#endif // LOGGEDINASTEACHER_H

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

LoggedInAsTeacher::LoggedInAsTeacher(LogginRegiste r *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))); //I need to send the idNr to this functions
ui->label_nameInfo->setText(QString::fromStdString(lr->TeacherDisplayName(t_idNr))); //And future functions in this form.
ui->label_userNameInfo->setText(QString::fromStdString(lr->TeacherDisplayUserName(t_idNr)));
}

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

Santosh Reddy
4th May 2017, 13:30
class LoggedInAsTeacher : public QDialog
{
public:
void SetId(int t_idNr);
};

jUno
4th May 2017, 13:57
Thank you for answering :)

But after writing the SetId function, and implementing it.
How do i get m_idNr from "mainwindow" to that Set function ?

Santosh Reddy
4th May 2017, 15:25
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->SetId(m_idNr); //<<<<<<<<<<<<
loggedTeacher->show();
}
else
{
QMessageBox::warning(this, "Login", "Username or password is incorrect");
}
}