[SOLVED] Issues with signals and slot to transfer data between two forms - Code included.



  • Thanks for taking time to read my post.

    I am having two forms (login window and a main app window). Based upon the user name and password entered in the loginwindow, I need to enable/disable certain widgets in my mainappwindow file.

    In my loginwindow form, I get user name and password. I check the database to determine the credentials for that data and pass these credentials (user, admin) to my mainapp. I tried using signals and slots but no data seems to be getting transferred.

    Attached below is a snippet of code.

    loginwindow.h
    @
    class loginwindow : public QDialog
    {
    Q_OBJECT

    public:
    explicit loginwindow(QWidget *parent = 0);
    ~loginwindow();
    QString getusr();
    QString getpass();
    signals:
    void sendcredentials(QString text);
    void sendstring();

    private slots:
    void on_login_clicked();

    private:
    Ui::loginwindow *ui;
    };
    @

    loginwindow.cpp
    @
    loginwindow::loginwindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::loginwindow)
    {
    ui->setupUi(this);
    }

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

    QString loginwindow::getusr()
    {
    return ui->user->text();
    }

    QString loginwindow::getpass()
    {
    return ui->pass->text();
    }

    void loginwindow::on_login_clicked()
    {
    QString credentials = "user";
    emit sendcredentials(credentials);
    //emit this->sendstring();
    qDebug()<<"Signal Generated";
    this->close();
    }
    @

    mainappwindow.h
    @
    class mainappwindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::mainappwindow *ui;

    public slots:
    void getcredentials(QString text);
    void getstring();
    };
    @

    mainappwindow.cpp
    @
    mainappwindow::mainappwindow(QWidget parent) :
    QMainWindow(parent),
    ui(new Ui::mainappwindow)
    {
    loginwindow
    window = new loginwindow;
    window->exec();
    QString usr = window->getusr();
    QString pass = window->getpass();

     ui->setupUi(this);
     connect(window,SIGNAL(sendcredentials(QString)),this,SLOT(getcredentials(QString)));
     connect(window,SIGNAL(sendstring()),this,SLOT(getstring()));
     qDebug()<<"Entered Here";
    

    }

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

    void mainappwindow::getcredentials(QString string)
    {
    qDebug()<<"Credentials"<<string;
    }

    void mainappwindow::getstring()
    {
    qDebug()<<"Hello World";
    }
    @

    Any help would be appreciated.

    How do I use signals and slots to transmit this data.

    [[marked up code, Tobias]]



  • Your main problem is in your ordering of actions. In your case, you first exec() your login window, and only after that has finished (remember, exec() is a blocking call!), you connect to the login windows signals. That is the wrong order of doing business.

    Also, I would recommend you don't do this from your mainwindow's constructor. Instead, you can trigger showing the login screen using a QTimer with 0 timeout. That means that your mainwindow first gets the opportunity to completely initialize, and only then your login dialog gets into action. Because you use signals and slots (good!), you can also use open() instead of exec(). open() does not block, so the code will just continue after the call immediately.



  • Thanks Andre.......


Log in to reply
 

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