Important: Please read the Qt Code of Conduct -

[Solved]login form enter key not working

  • Hi,
    I have a login form, when user enters user name and password it will open second form..but wen i click enter nothing happens...when i click enter key it should go to this to do it..please help
    void login::on_ok_clicked()
    if(ui->u_name->text()=="a" && ui->pwd->text()=="a")
    qWarning("Login Sucessful");
    ui->statusBar->showMessage("Login Failure");

  • In order to achieve that you can try


    check "QPushButton::setDefault()":

    Edit: This works for a QDialog

  • It didnt work i tried sam, I am using QMainWindow...Is there any way to make it work in qmainwindow or should i have to change to qdialog...?

  • It works for QDialog as per the documentation. In order to work for the QMainWindow you can override/handle the keyPressEvent().

  • It might be a good idea to use a QDialog to handle this. Then give the dialog a parent (your main window). You MainWindow file will then be must shorter (if you make the dialog in it's own cpp file etc). In the MainWindow you won't be bothered by the signals (Done in the QDialog) etc.
    Might look like this:
    QYourDialog LoginBox = new QYourDialog;
    if (LoginBox->exec() == QDialog::Accepted)
    if (LoginBox->u_name->text() == "a")
    // Oke??
    { // Wrong??

  • thanks jeroentje & sam

  • Did you get it working. If yes then Kindly edit your first post and add [Solved] to the title.

    Happy Coding!!!!

  • Ya working sam, used qdialog....

  • Dear Jeroentje,

    Would you mind to share the QYourDialog code?

    Thanks in advance.

Log in to reply