1

Introduction

Disclaimer: I am new to QT but who isn't learning.


I am creating a multi-window QT project in which windows need to communicate with each other. During the creation of this project, I was unsure of how to allow windows to communicate with each other. Luckily, I found this link: Passing Data Between Windows


After trying to implement it in my code, I found it wasn't responding and it wouldn't run after compiling.


Problem

I believe the problem lies in the code below.

Home window constructor

homeWindow::homeWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::homeWindow)
{
    ui->setupUi(this);
    /* Initialize Windows */
    loginWindow = new login;
    trainerW = new trainer;
    // Show the login window
    loginWindow->setFixedSize(400, 400);
    loginWindow->show();
    // Hide the current window
    this->setVisible(false);
    // Connect the calling of loginRequest in the loginWindow object to the visibility of the home window
    connect(loginWindow, SIGNAL(loginRequest()), this, SLOT(setVisible(true)));
}

Login Header File

 signals:
     void loginRequest();

Login CPP

#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QStatusBar>

void login::on_pushButton_login_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();

    if(username == "Test" && password == "test"){
        loginRequest();
    }else{
        ui->label_messegeBar->setText("Username or password is wrong.");
    }
}

Is there a better solution I should use? I am open to all feedback.

Thanks for everyones help,
macrocypher

Edit

I am still having issues launching the program. I made some changes. I decided to add all my code from relevant files in case there is a issue that I do not know.

Main.cpp

#include <QApplication>
#include "homewindow.h"

int mode = 1;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // Create homewindow object, calls login in constructer
    homeWindow hWindow;
    hWindow.start();
    return a.exec();
}

Homewindow.h

#ifndef HOMEWINDOW_H
#define HOMEWINDOW_H

#include <QMainWindow>
#include <trainer.h>
#include <login.h>

namespace Ui {
class homeWindow;
}

class homeWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit homeWindow(QWidget *parent = 0);
    ~homeWindow();
    void setUserName(QString username);
    void getTrainerData();
    void start();

private slots:
    void on_pushButton_learn_clicked();

private:
    login *loginWindow;
    Ui::homeWindow *ui;
    QString userName;
    trainer *trainerW;
};

#endif // HOMEWINDOW_H

Homewindow.cpp

#include "homewindow.h"
#include "ui_homewindow.h"
#include "trainer.h"
#include "session_adjust.h"
#include "login.h"
#include <QDebug>

homeWindow::homeWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::homeWindow)
{
    ui->setupUi(this);
    /* Initialize Windows */
    loginWindow = new login;
    trainerW = new trainer;
    // Hide the current window
    setVisible(false);
    // Connect the calling of loginRequest in the loginWindow object to the visibility of the home window
    connect (loginWindow, &login::loginRequest, this, &homeWindow::setVisible);
}

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

void homeWindow::setUserName(QString username)
{
    userName = username;
    ui->label_userName->setText(username);
}

void homeWindow::start()
{
    // Show the login window
    loginWindow->setFixedSize(400, 400);
    loginWindow->show();
}

void homeWindow::on_pushButton_learn_clicked()
{
    // When learn is clicked, start a new session
    trainerW->show();
    trainerW->startSession();
}

void getTrainerData() {
    //qDebug() << QString::number(trainerW->getIncorrectScore());
    //qDebug() << QString::number(trainerW->getCorrectScore());
}

login.h

#ifndef LOGIN_H
#define LOGIN_H

#include <QDialog>
//#include <homewindow.h>
//#include <session_adjust.h>

namespace Ui {
class login;
}

class login : public QDialog
{
    Q_OBJECT

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

signals:
    void loginRequest(bool);

private slots:
    void on_pushButton_login_clicked();
    void on_pushButton_quit_clicked();

private:
    Ui::login *ui;
    //session_adjust *sessionW;
    //homeWindow *hWindow;
};

#endif // LOGIN_H

login.cpp

#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QStatusBar>

login::login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    ui->label_messegeBar->setText("");
}

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

void login::on_pushButton_login_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();


    if(username == "Test" && password == "test"){

        /*hWindow = new homeWindow;
        hWindow->show();
        hWindow->setUserName(username);
        login::hide();*/



        /*
        sessionW = new session_adjust;
        sessionW->show();
        login::hide();
        */

        emit loginRequest(true);

    }else{
        //QMessageBox::warning(this, "Login", "Wrong username or password.");
        ui->label_messegeBar->setText("Username or password is wrong.");
        emit loginRequest(false);
    }
}

void login::on_pushButton_quit_clicked()
{
    QApplication::quit();
}
  • 1
    what's the problem exactly ? – Aminos Mar 13 '17 at 23:53
  • Is will not launch. It will open an icon on my hot bar but never fully runs. I am still having this issue after implementing goug's answer. –  Mar 14 '17 at 03:12
  • What do you mean by "never fully runs"? Does it exit or just appear stuck/unresponsive? If the latter then you should run it under a debugger and get a backtrace to find out what's going on. – G.M. Mar 14 '17 at 11:08
  • @macrocypher I ll try your code under my Qt 5.8 – Aminos Mar 14 '17 at 16:30
  • @macrocypher I tried your code and it works ! the login box (in my case I chose a QWidget instead of a QDialog maybe you should change it) is displayed correctly and then I can display the main window with the credentials Test:test,.. in Debug mode, put a break point in loginWindow->show(); after passing it, the login box should be displayed. Don't forget to run qmake too (it can sometimes solve some bizarre issues). – Aminos Mar 14 '17 at 23:06

2 Answers2

2

Your connect statement is incorrect; you can't put parameter values in there, only the types. Because of that, the connection is failing to be created, so nothing happens when you emit the signal. In the output from your program, you'll see an error. And you can't do this either:

connect(loginWindow, SIGNAL(loginRequest()), this, SLOT(setVisible(bool)));

Because there's no value for the parameter to setVisible. What you can do is change your loginRequest signal to take a parameter:

void loginRequest (bool visible);

Then the connect statement would be:

connect(loginWindow, SIGNAL(loginRequest(bool)), this, SLOT(setVisible(bool)));

And the emit would be:

emit loginRequest (true);

If you're using Qt 5 or later, a better syntax for the connect statement is:

connect (loginWindow, &login::loginRequest, this, &homeWindow::setVisible);

With this newer mechanism, problems are caught at compile time rather than run time.

goug
  • 2,294
  • 1
  • 11
  • 15
  • I am still facing issues running the application. Do you think you could help? All the information is posted in the question under the "Edit" heading. –  Mar 14 '17 at 04:16
  • ***All the information is posted in the question under the "Edit" heading.*** Remember that StackOverflow is not a forum. It is supposed to be 1 question per question not a dynamic question where the problem keeps changing until you get a working solution solving all the bugs in your program. The intent of this is to provide good questions and answers for future readers with the same problem. – drescherjm Mar 14 '17 at 17:00
0

Try a new UI class for the login : instead of a QDialog (dialog) choose a QWidget (widget).

Aminos
  • 754
  • 1
  • 20
  • 40