Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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 function..how to do it..please help
    [CODE]
    void login::on_ok_clicked()
    {
    if(ui->u_name->text()=="a" && ui->pwd->text()=="a")
    {
    //hide();
    qWarning("Login Sucessful");
    }
    else
    {
    ui->statusBar->showMessage("Login Failure");
    }
    }
    [/CODE]



  • In order to achieve that you can try

    @okButton->setDefault(true)@

    check "QPushButton::setDefault()":http://doc.qt.nokia.com/4.7-snapshot/qpushbutton.html#default-prop

    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??
    }
    else
    { // 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