Results 1 to 4 of 4

Thread: Sending int value between forms. From mainwindow to second dialog.

  1. #1
    Join Date
    May 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Sending int value between forms. From mainwindow to second dialog.

    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:n_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;
    }

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Sending int value between forms. From mainwindow to second dialog.

    Qt Code:
    1. class LoggedInAsTeacher : public QDialog
    2. {
    3. public:
    4. void SetId(int t_idNr);
    5. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 4th May 2017 at 14:36.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    May 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sending int value between forms. From mainwindow to second dialog.

    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 ?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Sending int value between forms. From mainwindow to second dialog.

    Qt Code:
    1. void MainWindow::on_pushButton_loggIn_clicked()
    2. {
    3. std::string username = ui->lineEdit_userName->text().toStdString();
    4. std::string password = ui->lineEdit_passWord->text().toStdString();
    5.  
    6. bool correctLoggIn = false;
    7. correctLoggIn = lr.loggInCheck(username, password);
    8.  
    9. if(correctLoggIn != false)
    10. {
    11. m_idNr = lr.GetTeacherId(username, password);
    12. this->hide();
    13. loggedTeacher->SetId(m_idNr); //<<<<<<<<<<<<
    14. loggedTeacher->show();
    15. }
    16. else
    17. {
    18. QMessageBox::warning(this, "Login", "Username or password is incorrect");
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. about dialog and mainwindow (ui,,ui)
    By Newbit in forum Newbie
    Replies: 0
    Last Post: 28th October 2014, 09:41
  2. how to resize the window forms and mainwindow as per the computer screen
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2012, 08:29
  3. Replies: 8
    Last Post: 16th July 2011, 00:10
  4. SEnding data between forms
    By core_st in forum Newbie
    Replies: 1
    Last Post: 31st January 2011, 10:23
  5. Crash during sending list from dialog to main window
    By Djony in forum Qt Programming
    Replies: 5
    Last Post: 23rd November 2006, 20:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.