Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to restrict the number of attempts in a login form ?
Qt 6.11 is out! See what's new in the release blog

How to restrict the number of attempts in a login form ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    hello everybody. i am making a login form. in that i have lineedit & lineedit_2 that take login id & password respectively. i want to restrict the maximum number of login attepts to three. also i want to display the message about the number of remaining attempts in label_3. i want that the checking starts the user hits a pushbutton. also to make sure that the changes to the value of loop counter "int i" persist between different function calls, i have used pass by reference. i have tried this code but it is becoming an infinite loop.
    @
    void D1::on_pushButton_clicked()
    {
    i= setval(&i);
    while(i<3)
    {
    if(!(ui->lineEdit->text().isEmpty()) && (!ui->lineEdit_2->text().isEmpty()))
    {
    ui->lineEdit_3->setText(QString::number(i));
    if((ui->lineEdit->text()=="admin") && (ui->lineEdit_2->text()=="admin"))
    {
    D2 d;
    d.exec();
    }
    else
    {
    ui->label_3->setText("Unsuccessful Attempt");
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    QString x= QString("the number of attempts left is %1").arg(i);
    i=chechval(&i);
    ui->label_3->setText(x);
    }
    }
    else
    {
    if(i==2)
    {
    ui->label_3->setText("Max number of attempts finished");
    }
    else
    {
    QString x= QString("the number of attempts left is %1").arg(i);
    i=chechval(&i);
    ui->label_3->setText(x);
    }
    }
    }
    }

    int D1::setval(int *ptr)
    {
    *ptr= 0;
    return(*ptr);
    }
    int D1::chechval(int *ptr)
    {
    *ptr= *ptr+1;
    return(*ptr);
    }

    @
    please tell me how can i improve upon this.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Please use "code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 in your post. This shall make the code sections readable.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        hello koahnig. i have modified the post as told by you. i must admit that the code looks really nice & readable now.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hmm, if I make a suggestion is to not have the D1 (I presume a dialog) start a new dialog (D2.exec), but rather have the D1 be started by the parent, wait for it to be closed or accepted, check the codes entered or maybe use the default return codes if the entry was valid and go and execute the D2 dialog, or go straight past it.
          @void D1::D1()
          {
          iTries = 1;
          ui->lineEdit->clear();
          ui->lineEdit_2->clear();
          ui->label_3->setText(QString("Attempt %1 of 3").arg(QString::number(iTries)));
          }

          void D1::on_pushButton_clicked()
          {
          // Check if log in was correct this time
          if ( (!ui->lineEdit->text().isEmpty()) &&
          (!ui->lineEdit_2->text().isEmpty()))
          {
          if((ui->lineEdit->text()=="admin") && (ui->lineEdit_2->text()=="admin"))
          {
          accept(); // this will close the dialog with an accept return code!
          }
          }

          // Check if the user did it too many times
          iTries++;
          if (iTries > 3)
          {
              QMessageBox::information(this, "Log in Failed!", "Number of attempts reached");
              reject();   // This will close the dialog and give a reject return code!
          }
          
          // setup the Gui for next login text
          ui->lineEdit->clear();
          ui->lineEdit_2->clear();
          ui->label_3->setText(QString("Attempt %1 of 3").arg(QString::number(iTries)));
          

          }

          // parent code:
          D1 * D1Dialog = new D1;
          
          if (D1Dialog->exec&#40;&#41; == QDialog::Accepted)
          {
              D2 * D2Dialog = new D2;
              D2->exec&#40;&#41;;
          }
          

          @

          It is also better practice to use the class pointer <name> = new class; for dialogs etc. This has to do with stack and heap stuff in the background. Don't be afraid to use the pointers. Qt is very great in code editor to replace a used . into a -> for members and to destroy the allocated data when the dialog of his parent is destroyed.
          Good luck with it.
          (btw, I didn't test the code, so no idea if it is perfect already)

          Greetz, Jeroen

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved