[SOLVED] Login check in main.cpp
-
Hi,
So you want to move the checking code in main ? Then you just have to add two getters to your dialog to retrieve the username and password. Then proceed with the check.
Hope it helps
-
Hi SGaist,
I have this in my login.cpp:
@
void Login::on_login_clicked()
{
QString user = ui->username->text();
QString passwd = ui->password->text();
qDebug() << "User is" << user;
qDebug() << "Password is" << passwd;
}
@And in my main.cpp I have this:
@
Login log(0);if (log.exec() == QDialog::Accepted) { qDebug() << "Password is" << Login->username->text(); //check function a.quit(); } else { qDebug() << "Wrong credential provided!"; }
@
I can't seem to get the value from login.cpp to main. Do you know what's wrong?
-
hi,
You're mixing stuff up. Making a variable Log (not a great name IYAM) of class type Login. After the exec() function you request member variables be returned form Login, not from log. Also use getters from like SGaist mentioned! Much better then public member variables!I'll rewrite it in main.cpp
@
LoginDialog MyLogIn_p = new LoginDialog(this);
if (MyLogIn_p->exec() == QDialog::Accepted)
{
qDebug() << "User: " << MyLogIn_p->GetUserName();
qDebug() << "PassWord: " << MyLogIn_p->GetPassWord();
}
// No need to delete the MyLogIn_p class, because of the parent (this) it will be removed by Qt when it is out of scope.
@
Of course does your dialog get two methodes: GetUserName and GetPassWord that return a QString. -
Hmm, if you're still in main.cpp, did you start your application already?
The eventQueque need to be run to handle the QDialog stuff. -
exec runs an internal event loop so it should be good from that point of view
-
Oh,
Also when the QApplication is not running?checking docs <<
Ah, yes, it will start a local event loop ;-)
Never too old to learn! -
Do you mean something like
@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);QDialog dialog;
dialog.exec();If (some condition) {
QWidget widget;
widget.show();return app.exec();
}
return -1;
}
@?
-
Hi,
For the getters, I'm not sure if this is the right way to do but here's what i have in login.cpp
@
QObject::connect(ui->username, SIGNAL(textChanged(QString)), ui->username, SLOT(GetUserLogin()));
QObject::connect(ui->password, SIGNAL(textChanged(QString)), ui->password, SLOT(GetPassWord()));void Login::GetUserLogin(const QString user)
{QString userlogin = user; qDebug() << "User is" << userlogin;
}
void Login::GetPassWord (const QString passwd)
{QString passwdlogin = passwd;
qDebug() << "passwd is" << passwdlogin;
}
@It runs but it get this message:
QObject::connect: No such slot QLineEdit::GetUserLogin() in ..\ChineseNMT\login.cpp:18
QObject::connect: (sender name: 'username')
QObject::connect: (receiver name: 'username')
QObject::connect: No such slot QLineEdit::GetPassWord() in ..\ChineseNMT\login.cpp:19
QObject::connect: (sender name: 'password')
QObject::connect: (receiver name: 'password') -
Getters are not slots, just function returning something.
You should first take a look at some of the tutorials before going further, that will give you a good base to build on
-
Hi,
I'm sorry having to ask this again, I got my checking in main.cpp done but i noticed that it's looping twice. I'm not sure of where the loop is and I'm hoping that someone could help shed some light on this. Thanks very much!login.cpp:
@
Login::Login(QWidget *parent) :
QDialog(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
this->setWindowTitle("Login");
QObject::connect(ui->login, SIGNAL(clicked()), this, SLOT(on_login_clicked()));}
void Login::on_login_clicked()
{QString user = ui->username->text(); QString passwd = ui->password->text();
if ((!ui->username->text().isEmpty()) && (!ui->password->text().isEmpty()))
{
QMessageBox msgBox;
//call checking from main.cpp
MainWindow mw;
mw.checkLogin(user, passwd);
if (mw.checkLogin(user, passwd) == 0)
{
qDebug() << "Success! Close dialog box";
QDialog::close();
}else
{
msgBox.setText("Login FAILED! Try again!");
msgBox.exec();
}
}
else
{
qDebug() << "Invalid credential provided!";
}
}@
main.cpp
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);MainWindow w; w.showMaximized(); Login MyLogIn; MyLogIn.exec(); return a.exec();
}
int MainWindow::checkLogin(QString a, QString b)
{QString aa = a; QString bb = b; if((aa=="admin") && (bb=="1234")) { qDebug() << "Login success!"; return 0; } else { qDebug() << "Login FAILED!"; return 1; }
}
@result:
Login success!
Login success!
Success! Close dialog box
Login success!
Login success!
Success! Close dialog box -
@
//call checking from main.cpp
MainWindow mw; << it's not the MainWindow from main.cpp
mw.checkLogin(user, passwd);
if (mw.checkLogin(user, passwd) == 0)
@ -
thanks SGaist, I have changed my program the other round and now it's working without the double login.
Thanks very much for looking into this.