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