I have a basic QT Creator application with a main page called MyBroker, and a button that switches it to the Signin Page. The main page outputs and functions correctly, but the Signin Page does not. The UI itself functions fine but nothing is outputted, including log statements. I have tried revisiting everything ChatGPT and I could think of but nothing seems to work.
I did not include the ui file but all the members between the ui and source/header files check out, and the signup_ui.h is created. Also the projects builds and deploys.
I also have mongocxx drivers installed but that should not affect anything with the basic issue.
MyBroker.cpp
#include "mybroker.h"
#include "ui_mybroker.h"
#include "ui_login.h"
#include "ui_signup.h"
#include "mongohandler.h"
#include <iostream>
MyBroker::MyBroker(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MyBroker),
Signupui(nullptr),
Loginui(nullptr),
currentWidget(nullptr)
{
ui->setupUi(this);
connect(ui->loginButton,&QPushButton::clicked,this, [=](){
SwitchUI("Login");
});
connect(ui->signUpButton,&QPushButton::clicked,this, [=](){
SwitchUI("Signup");
});
}
MyBroker::~MyBroker()
{
delete ui;
if (Signupui) {
delete Signupui;
}
if (Loginui) {
delete Loginui;
}
delete currentWidget;
}
void MyBroker::SwitchUI(const QString &newUI) {
std::cout << "Switch" << std::endl;
if (currentWidget) {
delete currentWidget;
currentWidget = nullptr;
}
currentWidget = new QWidget(this);
if (newUI == "Signup") {
Signupui = new Ui::Signup;
Signupui->setupUi(currentWidget);
}
else if (newUI == "Login") {
Loginui = new Ui::Login;
Loginui->setupUi(currentWidget);
}
setCentralWidget(currentWidget);
}
bool MyBroker::eventFilter(QObject *obj, QEvent *event) {
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(obj);
if (lineEdit) {
if (event->type() == QEvent::FocusIn) {
lineEdit->clear(); // Clear text when the field gains focus
} else if (event->type() == QEvent::FocusOut) {
if (lineEdit->text().isEmpty()) {
lineEdit->clear(); // Clear the placeholder text if the field is empty
}
}
}
return QMainWindow::eventFilter(obj, event);
}
signup.cpp
#include "signup.h"
#include "ui_signup.h"
#include "mongohandler.h"
#include <iostream>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
Signup::Signup(QWidget *parent)
: MyBroker(parent),
ui(new Ui::Signup)
{
ui->setupUi(this);
if (ui->SignupButton) {
std::cout << "SignupButton exists" << std::endl;
}
connect(ui->SignupButton, &QPushButton::clicked, this, &Signup::checkInfo);
}
Signup::~Signup()
{
delete ui;
}
QString Signup::getText(const QString type) const {
QLineEdit *lineEdit = findChild<QLineEdit*>(type);
if (lineEdit) {
return lineEdit->text();
}
return QString();
}
bool Signup::checkInfo() {
std::cout << "Signup button clicked" << std::endl;
MongoDBHandler handler = connectMongo();
QString password = getText("signupPassword");
QString confirmPassword = getText("confirmPassword");
QString email = getText("signupEmail");
QString username = getText("signupUsername");
QString firstName = getText("firstName");
QString lastName = getText("lastName");
QStringList info = {password, confirmPassword, email, username, firstName, lastName};
bool error = false;
for (const QString &field : info) {
if (field.isEmpty()) {
error = true;
errorHandle("emptyField");
}
}
if (password != confirmPassword) {
errorHandle("mismatchPassword");
error = true;
}
if (handler.Exists("email", email.toStdString())) {
errorHandle("emailExists");
error = true;
}
if (handler.Exists("username", username.toStdString())) {
errorHandle("usernameExists");
error = true;
}
if (error) {
return false;
} else {
errorHandle("none");
// Assuming you want to switch to login UI after successful signup
// Emit a signal or call a method to switch UI
return true;
}
}
void Signup::errorHandle(QString error) {
if (error == "emptyField") {
ui->errorText->setText("Please fill in all fields.");
} else if (error == "usernameExists") {
ui->errorText->setText("Username already exists.");
} else if (error == "emailExists") {
ui->errorText->setText("Email is already used.");
} else if (error == "mismatchPassword") {
ui->errorText->setText("Passwords do not match.");
} else if (error == "none") {
ui->errorText->setText("");
}
}
MongoDBHandler Signup::connectMongo() {
MongoDBHandler handler;
if (!handler.connect(connect, "MyBroker", "Login")) {
std::cerr << "Failed to connect to the database" << std::endl;
} else {
std::cout << "Connected to the database" << std::endl;
}
return handler;
}
signup.h
#ifndef SIGNUP_H
#define SIGNUP_H
#include "qmainwindow.h"
#include <QWidget>
#include "ui_signup.h"
#include "ui_login.h"
#include "mongohandler.h"
#include "mybroker.h"
namespace Ui {
class Signup;
class Login;
}
class Signup : public MyBroker
{
Q_OBJECT
public:
explicit Signup(QWidget *parent = nullptr);
QString getText(QString type) const;
bool checkInfo();
void errorHandle(QString error);
void sendInfo();
MongoDBHandler connectMongo();
~Signup();
private:
Ui::Signup *ui;
Ui::Login *Loginui;
QWidget *currentWidget;
QString lastName;
QString firstName;
QString email;
QString username;
QString password;
};
#endif // SIGNUP_H
I tried configuring the .pro file, including more header/source files than needed, and troubleshooting/debugging but I cannot figure out whats wrong. ChatGPT, which has usually worked well with debugging basic QT Creator issues cannot figure it out aswell.