User Login Dialog: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Cleanup)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:snippets]]
{{LangSwitch}}
[[Category:HowTo]]
[[Category:snippets]]
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.


= An example of user login dialog =
== Header file ==


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.
<code>
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QComboBox>
#include <QGridLayout>
#include <QStringList>
#include <QDebug>


== Header file ==
/*!
'''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;


<code><br />#include &lt;QDialog&amp;gt;<br />#include &lt;QLabel&amp;gt;<br />#include &lt;QPushButton&amp;gt;<br />#include &lt;QDialogButtonBox&amp;gt;<br />#include &lt;QLineEdit&amp;gt;<br />#include &lt;QComboBox&amp;gt;<br />#include &lt;QGridLayout&amp;gt;<br />#include &lt;QStringList&amp;gt;<br />#include &lt;QDebug&amp;gt;
/*!
* A label for the password.
*/
QLabel''' labelPassword;


/*!<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
/*!
* An editable combo box for allowing the user
* to enter his username or select it from a list.
*/
QComboBox''' comboUsername;


private:<br /> /*!<br /> * A label for the username component.<br /> '''/<br /> QLabel''' labelUsername;
/*!
* A field to let the user enters his password.
*/
QLineEdit''' editPassword;


/*!<br /> * A label for the password.<br /> '''/<br /> QLabel''' labelPassword;
/*!
* The standard dialog button box.
*/
QDialogButtonBox''' buttons;


/*!<br /> * An editable combo box for allowing the user<br /> * to enter his username or select it from a list.<br /> '''/<br /> QComboBox''' comboUsername;
/*!
* A method to set up all dialog components and
* initialize them.
*/
void setUpGUI();


/*!<br /> * A field to let the user enters his password.<br /> '''/<br /> QLineEdit''' editPassword;
public:
explicit LoginDialog(QWidget '''parent = 0);


/*!<br /> * The standard dialog button box.<br /> '''/<br /> QDialogButtonBox''' buttons;
/*!
* 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 );


/*!<br /> * A method to set up all dialog components and<br /> * initialize them.<br /> */<br /> void setUpGUI();
/*!
* 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 );


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;amp; username );
* Sets a list of allowed usernames from which the user
<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;amp; password );
* can pick one if he does not want to directly edit it.
<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;amp; usernames );
'''usernames a list of usernames
*/
void setUsernamesList( const QStringList& usernames );


signals:
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;amp; username, QString&amp;amp; password, int&amp;amp; indexNumber );
/*!
<br />public slots:<br /> /'''!<br /> * A lot to adjust the emitting of the signal.<br /> */<br /> void slotAcceptLogin();
* 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();


};
};
Line 39: Line 106:
== Implementation ==
== Implementation ==


<code>LoginDialog::LoginDialog(QWidget '''parent) :<br /> QDialog(parent)<br />{<br /> setUpGUI();<br /> setWindowTitle( tr(&quot;User Login&amp;quot;) );<br /> setModal( true );<br />}
<code>LoginDialog::LoginDialog(QWidget '''parent) :
<br />void LoginDialog::setUpGUI(){<br /> // set up the layout<br /> QGridLayout''' formGridLayout = new QGridLayout( this );
QDialog(parent)
{
setUpGUI();
setWindowTitle( tr("User Login") );
setModal( true );
}


// initialize the username combo box so that it is editable<br /> comboUsername = new QComboBox( this );<br /> comboUsername-&gt;setEditable( true );<br /> // initialize the password field so that it does not echo<br /> // characters<br /> editPassword = new QLineEdit( this );<br /> editPassword-&gt;setEchoMode( QLineEdit::Password );
void LoginDialog::setUpGUI(){
// set up the layout
QGridLayout''' formGridLayout = new QGridLayout( this );


// initialize the labels<br /> labelUsername = new QLabel( this );<br /> labelPassword = new QLabel( this );<br /> labelUsername-&gt;setText( tr( &quot;Username&amp;quot; ) );<br /> labelUsername-&gt;setBuddy( comboUsername );<br /> labelPassword-&gt;setText( tr( &quot;Password&amp;quot; ) );<br /> labelPassword-&gt;setBuddy( editPassword );
// 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 buttons<br /> buttons = new QDialogButtonBox( this );<br /> buttons-&gt;addButton( QDialogButtonBox::Ok );<br /> buttons-&gt;addButton( QDialogButtonBox::Cancel );<br /> buttons-&gt;button( QDialogButtonBox::Ok )<s>&gt;setText( tr(&quot;Login&amp;quot;) );<br /> buttons</s>&gt;button( QDialogButtonBox::Cancel )<s>&gt;setText( tr(&quot;Abort&amp;quot;) );
// initialize the labels
<br /> // connects slots<br /> connect( buttons</s>&gt;button( QDialogButtonBox::Cancel ),<br /> SIGNAL (clicked()),<br /> this,<br /> SLOT (close())<br /> );
labelUsername = new QLabel( this );
labelPassword = new QLabel( this );
labelUsername->setText( tr( "Username" ) );
labelUsername->setBuddy( comboUsername );
labelPassword->setText( tr( "Password" ) );
labelPassword->setBuddy( editPassword );


connect( buttons-&gt;button( QDialogButtonBox::Ok ),<br /> SIGNAL (clicked()),<br /> this,<br /> SLOT (slotAcceptLogin()) );
// 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<br /> formGridLayout-&gt;addWidget( labelUsername, 0, 0 );<br /> formGridLayout-&gt;addWidget( comboUsername, 0, 1 );<br /> formGridLayout-&gt;addWidget( labelPassword, 1, 0 );<br /> formGridLayout-&gt;addWidget( editPassword, 1, 1 );<br /> formGridLayout-&gt;addWidget( buttons, 2, 0, 1, 2 );
// 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 164:
}
}


void LoginDialog::setUsername(QString &amp;username){<br /> bool found = false;<br /> for( int i = 0; i &lt; comboUsername-&gt;count() &amp;&amp; ! found ; i++ )<br /> if( comboUsername-&gt;itemText( i ) == username ){<br /> comboUsername-&gt;setCurrentIndex( i );<br /> found = true;<br /> }
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 ){<br /> int index = comboUsername-&gt;count();<br /> qDebug() &lt;&lt; &quot;Select username &quot; &lt;&lt; index;<br /> comboUsername-&gt;addItem( username );
if( ! found ){
int index = comboUsername->count();
qDebug() << "Select username " << index;
comboUsername->addItem( username );


comboUsername-&gt;setCurrentIndex( index );<br /> }
comboUsername->setCurrentIndex( index );
}


// place the focus on the password field<br /> editPassword-&gt;setFocus();<br />}
// place the focus on the password field
editPassword->setFocus();
}


void LoginDialog::setPassword(QString &amp;password){<br /> editPassword-&gt;setText( password );<br />}
void LoginDialog::setPassword(QString &password){
editPassword->setText( password );
}


void LoginDialog::slotAcceptLogin(){<br /> QString username = comboUsername-&gt;currentText();<br /> QString password = editPassword-&gt;text();<br /> int index = comboUsername-&gt;currentIndex();
void LoginDialog::slotAcceptLogin(){
QString username = comboUsername->currentText();
QString password = editPassword->text();
int index = comboUsername->currentIndex();


emit acceptLogin( username, // current username<br /> password, // current password<br /> index // index in the username list<br /> );
emit acceptLogin( username, // current username
password, // current password
index // index in the username list
);


// close this dialog<br /> close();<br />}
// close this dialog
close();
}


void LoginDialog::setUsernamesList(const QStringList &amp;usernames){<br /> comboUsername-&gt;addItems( usernames );<br />}
void LoginDialog::setUsernamesList(const QStringList &usernames){
comboUsername->addItems( usernames );
}


</code>
</code>
Line 79: Line 210:
== Usage example ==
== Usage example ==


<code>LoginDialog* loginDialog = new LoginDialog( this );<br />loginDialog-&gt;setUsername( &quot;Luca&amp;quot; ); // optional<br />connect( loginDialog,<br /> SIGNAL (acceptLogin(QString&amp;amp;,QString&amp;amp;,int&amp;amp;)),<br /> this,<br /> SLOT (slotAcceptUserLogin(QString&amp;amp;,QString&amp;amp;)));<br />loginDialog-&gt;exec&amp;amp;#40;&amp;#41;;
<code>LoginDialog* loginDialog = new LoginDialog( this );
loginDialog->setUsername( "Luca" ); // optional
connect( loginDialog,
SIGNAL (acceptLogin(QString&,QString&,int&)),
this,
SLOT (slotAcceptUserLogin(QString&,QString&)));
loginDialog->exec();


</code>
</code>

Latest revision as of 00:03, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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();