Difference between revisions of "User Login Dialog"
Line 1: | Line 1: | ||
− | [[Category:HowTo]] | + | [[Category:HowTo]] |
+ | [[Category:snippets]] | ||
= An example of user login dialog = | = An example of user login dialog = | ||
− | This code snippets shows how to implement a simple dialog window that allows the user to enter his username and password in order to make a login attempt. The dialog window will emit a signal in order to notify a listener for the login credentials. The login dialog can also presents a list of usernames to select one from. | + | This code snippets shows how to implement a simple dialog window that allows the user to enter his username and password in order to make a login attempt. The dialog window will emit a signal in order to notify a listener for the login credentials. The login dialog can also presents a list of usernames to select one from. |
+ | The dialog also allows for the proposal of a specified username/password as it could be loaded from a settings or a configuration file. | ||
== Header file == | == Header file == | ||
− | <code> | + | <code> |
+ | #include <QDialog> | ||
+ | #include <QLabel> | ||
+ | #include <QPushButton> | ||
+ | #include <QDialogButtonBox> | ||
+ | #include <QLineEdit> | ||
+ | #include <QComboBox> | ||
+ | #include <QGridLayout> | ||
+ | #include <QStringList> | ||
+ | #include <QDebug> | ||
− | /*! | + | /*! |
+ | '''Makes class LoginDialog a child to its parent, QDialog | ||
+ | */ | ||
+ | class LoginDialog : public QDialog | ||
+ | { | ||
+ | /*! | ||
+ | '''Turns Login Dialog into a QObject | ||
+ | */ | ||
+ | Q_OBJECT | ||
− | private: | + | private: |
+ | /*! | ||
+ | * A label for the username component. | ||
+ | */ | ||
+ | QLabel''' labelUsername; | ||
− | /*! | + | /*! |
+ | * A label for the password. | ||
+ | */ | ||
+ | QLabel''' labelPassword; | ||
− | /*! | + | /*! |
+ | * An editable combo box for allowing the user | ||
+ | * to enter his username or select it from a list. | ||
+ | */ | ||
+ | QComboBox''' comboUsername; | ||
− | /*! | + | /*! |
+ | * A field to let the user enters his password. | ||
+ | */ | ||
+ | QLineEdit''' editPassword; | ||
− | /*! | + | /*! |
+ | * The standard dialog button box. | ||
+ | */ | ||
+ | QDialogButtonBox''' buttons; | ||
− | /*! | + | /*! |
+ | * A method to set up all dialog components and | ||
+ | * initialize them. | ||
+ | */ | ||
+ | void setUpGUI(); | ||
− | public: | + | public: |
− | + | explicit LoginDialog(QWidget '''parent = 0); | |
− | + | ||
− | + | /*! | |
+ | * Sets the proposed username, that can come for instance | ||
+ | * from a shared setting. | ||
+ | '''username the string that represents the current username | ||
+ | ''' to display | ||
+ | */ | ||
+ | void setUsername( QString&amp; username ); | ||
+ | |||
+ | /*! | ||
+ | * Sets the current password to propose to the user for the login. | ||
+ | * password the password to fill into the dialog form | ||
+ | */ | ||
+ | void setPassword( QString&amp; password ); | ||
+ | |||
+ | /*! | ||
+ | * Sets a list of allowed usernames from which the user | ||
+ | * can pick one if he does not want to directly edit it. | ||
+ | '''usernames a list of usernames | ||
+ | */ | ||
+ | void setUsernamesList( const QStringList&amp; usernames ); | ||
signals: | signals: | ||
− | /*! | + | /*! |
− | + | * A signal emitted when the login is performed. | |
+ | * username the username entered in the dialog | ||
+ | * password the password entered in the dialog | ||
+ | * index the number of the username selected in the combobox | ||
+ | */ | ||
+ | void acceptLogin( QString&amp; username, QString&amp; password, int&amp; indexNumber ); | ||
+ | |||
+ | public slots: | ||
+ | /*! | ||
+ | * A lot to adjust the emitting of the signal. | ||
+ | */ | ||
+ | void slotAcceptLogin(); | ||
}; | }; | ||
Line 39: | Line 109: | ||
== Implementation == | == Implementation == | ||
− | <code>LoginDialog::LoginDialog(QWidget '''parent) : | + | <code>LoginDialog::LoginDialog(QWidget '''parent) : |
− | + | QDialog(parent) | |
+ | { | ||
+ | setUpGUI(); | ||
+ | setWindowTitle( tr("User Login") ); | ||
+ | setModal( true ); | ||
+ | } | ||
− | + | void LoginDialog::setUpGUI(){ | |
+ | // set up the layout | ||
+ | QGridLayout''' formGridLayout = new QGridLayout( this ); | ||
− | // initialize the | + | // initialize the username combo box so that it is editable |
+ | comboUsername = new QComboBox( this ); | ||
+ | comboUsername->setEditable( true ); | ||
+ | // initialize the password field so that it does not echo | ||
+ | // characters | ||
+ | editPassword = new QLineEdit( this ); | ||
+ | editPassword->setEchoMode( QLineEdit::Password ); | ||
− | // initialize | + | // initialize the labels |
− | + | labelUsername = new QLabel( this ); | |
+ | labelPassword = new QLabel( this ); | ||
+ | labelUsername->setText( tr( "Username" ) ); | ||
+ | labelUsername->setBuddy( comboUsername ); | ||
+ | labelPassword->setText( tr( "Password" ) ); | ||
+ | labelPassword->setBuddy( editPassword ); | ||
− | + | // initialize buttons | |
+ | buttons = new QDialogButtonBox( this ); | ||
+ | buttons->addButton( QDialogButtonBox::Ok ); | ||
+ | buttons->addButton( QDialogButtonBox::Cancel ); | ||
+ | buttons->button( QDialogButtonBox::Ok )->setText( tr("Login") ); | ||
+ | buttons->button( QDialogButtonBox::Cancel )->setText( tr("Abort") ); | ||
− | // place components into the dialog | + | // connects slots |
+ | connect( buttons->button( QDialogButtonBox::Cancel ), | ||
+ | SIGNAL (clicked()), | ||
+ | this, | ||
+ | SLOT (close()) | ||
+ | ); | ||
+ | |||
+ | connect( buttons->button( QDialogButtonBox::Ok ), | ||
+ | SIGNAL (clicked()), | ||
+ | this, | ||
+ | SLOT (slotAcceptLogin()) ); | ||
+ | |||
+ | // place components into the dialog | ||
+ | formGridLayout->addWidget( labelUsername, 0, 0 ); | ||
+ | formGridLayout->addWidget( comboUsername, 0, 1 ); | ||
+ | formGridLayout->addWidget( labelPassword, 1, 0 ); | ||
+ | formGridLayout->addWidget( editPassword, 1, 1 ); | ||
+ | formGridLayout->addWidget( buttons, 2, 0, 1, 2 ); | ||
setLayout( formGridLayout ); | setLayout( formGridLayout ); | ||
Line 57: | Line 167: | ||
} | } | ||
− | void LoginDialog::setUsername(QString &username){ | + | void LoginDialog::setUsername(QString &username){ |
+ | bool found = false; | ||
+ | for( int i = 0; i < comboUsername->count() && ! found ; i++ ) | ||
+ | if( comboUsername->itemText( i ) == username ){ | ||
+ | comboUsername->setCurrentIndex( i ); | ||
+ | found = true; | ||
+ | } | ||
− | if( ! found ){ | + | if( ! found ){ |
+ | int index = comboUsername->count(); | ||
+ | qDebug() << "Select username " << index; | ||
+ | comboUsername->addItem( username ); | ||
− | comboUsername- | + | comboUsername->setCurrentIndex( index ); |
+ | } | ||
− | // place the focus on the password field | + | // place the focus on the password field |
+ | editPassword->setFocus(); | ||
+ | } | ||
− | void LoginDialog::setPassword(QString &password){ | + | void LoginDialog::setPassword(QString &password){ |
+ | editPassword->setText( password ); | ||
+ | } | ||
− | void LoginDialog::slotAcceptLogin(){ | + | void LoginDialog::slotAcceptLogin(){ |
+ | QString username = comboUsername->currentText(); | ||
+ | QString password = editPassword->text(); | ||
+ | int index = comboUsername->currentIndex(); | ||
− | emit acceptLogin( username, // current username | + | emit acceptLogin( username, // current username |
+ | password, // current password | ||
+ | index // index in the username list | ||
+ | ); | ||
− | // close this dialog | + | // close this dialog |
+ | close(); | ||
+ | } | ||
− | void LoginDialog::setUsernamesList(const QStringList &usernames){ | + | void LoginDialog::setUsernamesList(const QStringList &usernames){ |
+ | comboUsername->addItems( usernames ); | ||
+ | } | ||
</code> | </code> | ||
Line 79: | Line 213: | ||
== Usage example == | == Usage example == | ||
− | <code>LoginDialog* loginDialog = new LoginDialog( this ); | + | <code>LoginDialog* loginDialog = new LoginDialog( this ); |
+ | loginDialog->setUsername( "Luca" ); // optional | ||
+ | connect( loginDialog, | ||
+ | SIGNAL (acceptLogin(QString&amp;,QString&amp;,int&amp;)), | ||
+ | this, | ||
+ | SLOT (slotAcceptUserLogin(QString&amp;,QString&amp;))); | ||
+ | loginDialog->exec(); | ||
</code> | </code> |
Revision as of 08:57, 25 February 2015
An example of user login dialog
This code snippets shows how to implement a simple dialog window that allows the user to enter his username and password in order to make a login attempt. The dialog window will emit a signal in order to notify a listener for the login credentials. The login dialog can also presents a list of usernames to select one from. The dialog also allows for the proposal of a specified username/password as it could be loaded from a settings or a configuration file.
Header file
- include <QDialog>
- include <QLabel>
- include <QPushButton>
- include <QDialogButtonBox>
- include <QLineEdit>
- include <QComboBox>
- include <QGridLayout>
- include <QStringList>
- include <QDebug>
/*!
Makes class LoginDialog a child to its parent, QDialog
- /
class LoginDialog : public QDialog
{
/*!
Turns Login Dialog into a QObject
- /
Q_OBJECT
private:
/*!
* A label for the username component.
*/
QLabel labelUsername;
/*!
* A label for the password.
*/
QLabel labelPassword;
/*!
* An editable combo box for allowing the user
* to enter his username or select it from a list.
*/
QComboBox comboUsername;
/*!
* A field to let the user enters his password.
*/
QLineEdit editPassword;
/*!
* The standard dialog button box.
*/
QDialogButtonBox buttons;
/*!
* A method to set up all dialog components and
* initialize them.
*/
void setUpGUI();
public:
explicit LoginDialog(QWidget parent = 0);
/*!
* Sets the proposed username, that can come for instance
* from a shared setting.
username the string that represents the current username
to display
*/
void setUsername( QString& username );
/*!
* Sets the current password to propose to the user for the login.
* password the password to fill into the dialog form
*/
void setPassword( QString& password );
/*!
* Sets a list of allowed usernames from which the user
* can pick one if he does not want to directly edit it.
usernames a list of usernames
- /
void setUsernamesList( const QStringList& usernames );
signals:
/*!
* A signal emitted when the login is performed.
* username the username entered in the dialog
* password the password entered in the dialog
* index the number of the username selected in the combobox
*/
void acceptLogin( QString& username, QString& password, int& indexNumber );
public slots:
/*!
* A lot to adjust the emitting of the signal.
*/
void slotAcceptLogin();
};
endif // LOGINDIALOG_H
Implementation
LoginDialog::LoginDialog(QWidget parent) :
QDialog(parent)
{
setUpGUI();
setWindowTitle( tr("User Login") );
setModal( true );
}
void LoginDialog::setUpGUI(){
// set up the layout
QGridLayout formGridLayout = new QGridLayout( this );
// initialize the username combo box so that it is editable
comboUsername = new QComboBox( this );
comboUsername->setEditable( true );
// initialize the password field so that it does not echo
// characters
editPassword = new QLineEdit( this );
editPassword->setEchoMode( QLineEdit::Password );
// initialize the labels
labelUsername = new QLabel( this );
labelPassword = new QLabel( this );
labelUsername->setText( tr( "Username" ) );
labelUsername->setBuddy( comboUsername );
labelPassword->setText( tr( "Password" ) );
labelPassword->setBuddy( editPassword );
// initialize buttons
buttons = new QDialogButtonBox( this );
buttons->addButton( QDialogButtonBox::Ok );
buttons->addButton( QDialogButtonBox::Cancel );
buttons->button( QDialogButtonBox::Ok )->setText( tr("Login") );
buttons->button( QDialogButtonBox::Cancel )->setText( tr("Abort") );
// connects slots
connect( buttons->button( QDialogButtonBox::Cancel ),
SIGNAL (clicked()),
this,
SLOT (close())
);
connect( buttons->button( QDialogButtonBox::Ok ),
SIGNAL (clicked()),
this,
SLOT (slotAcceptLogin()) );
// place components into the dialog
formGridLayout->addWidget( labelUsername, 0, 0 );
formGridLayout->addWidget( comboUsername, 0, 1 );
formGridLayout->addWidget( labelPassword, 1, 0 );
formGridLayout->addWidget( editPassword, 1, 1 );
formGridLayout->addWidget( buttons, 2, 0, 1, 2 );
setLayout( formGridLayout );
}
void LoginDialog::setUsername(QString &username){
bool found = false;
for( int i = 0; i < comboUsername->count() && ! found ; i++ )
if( comboUsername->itemText( i ) == username ){
comboUsername->setCurrentIndex( i );
found = true;
}
if( ! found ){
int index = comboUsername->count();
qDebug() << "Select username " << index;
comboUsername->addItem( username );
comboUsername->setCurrentIndex( index );
}
// place the focus on the password field
editPassword->setFocus();
}
void LoginDialog::setPassword(QString &password){
editPassword->setText( password );
}
void LoginDialog::slotAcceptLogin(){
QString username = comboUsername->currentText();
QString password = editPassword->text();
int index = comboUsername->currentIndex();
emit acceptLogin( username, // current username
password, // current password
index // index in the username list
);
// close this dialog
close();
}
void LoginDialog::setUsernamesList(const QStringList &usernames){
comboUsername->addItems( usernames );
}
Usage example
LoginDialog* loginDialog = new LoginDialog( this );
loginDialog->setUsername( "Luca" ); // optional
connect( loginDialog,
SIGNAL (acceptLogin(QString&,QString&,int&)),
this,
SLOT (slotAcceptUserLogin(QString&,QString&)));
loginDialog->exec();