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