0

I'm making a form where a user submits their own name, username, etc. but the username must be unique, so it throws up a QMessageBox error if the chosen username is already in use.

If that lineEdit is not first in the tab order, how do you snap to that lineEdit after the click event?

{
    UserDB userconn;
    QString name,username,password,number,userid,userid2;
    name=ui->lineEdit_firstlastname->text();
    username=ui->lineEdit_username->text();
    password=ui->lineEdit_password->text();
    number=ui->lineEdit_phonenumber->text();
    userid="Admin";
    userid2="User";

    QByteArray prehash;
    prehash.append(password);
    QString hashword = QString(QCryptographicHash::hash(prehash,QCryptographicHash::Md5).toHex());

    QSqlQuery qry2;
    qry2.prepare("select * from user where username='"+username+"'");
    if(qry2.exec())
    {
        int count=0;
        while(qry2.next())\
        {
            count++;
        }
        if(count==1)
        {
            QMessageBox::critical(this,tr("Error!"),tr("Choose a different username!"));
            ui->lineEdit_username->setText("");
        }
        else....
        {
          /* 
             here, if the user submits a duplicate username, a 
             QMessageBox pops up telling the user to choose a 
             different username, and the focus returns to either the 
             pushButton or to the last lineEdit before the user 
             pressed Enter, and in this case, I'd like to return the 
             focus to that lineEdit that needs to be edited.
          */
        }
    }
}
Stanley F.
  • 1,846
  • 16
  • 29

1 Answers1

0

What you are looking for is the Qt function setFocus().

Another stackoverflow thread: Set QLineEdit focus in Qt provides different answers on how to use it.

Mariam
  • 342
  • 3
  • 18
  • There are many ways of doing this. You can subclass qlineedit and make a slot that toggles boarder color to red triggered by the signal editingFinished or returnPressed or something and check against a list, or have a trigger in the parent that sets the focus on a particular lineedit based on value when the page is ready. – Mike Aug 24 '18 at 19:47
  • This wasnt supposed to be a comment on your answer but oh well – Mike Aug 24 '18 at 19:48