Доброго всем времени суток всем. Для маленькой программы нужно было сделать авторизацию при входе. Сделать-то я ее сделал, но как-то оно мне не очень нравится, кажется, что не по «фэншую» получилось. Посему прошу, если не затруднит, конструктивной критики и дельных советов.
#ifndef AUTHDIALOG_HPP
#define AUTHDIALOG_HPP
#include <QtGui>
class AuthDialog : public QDialog {
Q_OBJECT
public:
AuthDialog(QWidget *parent = 0);
~AuthDialog();
private slots:
void onAcceptButtonPressed();
void onAcceptErrorButtonPressed();
void moveFocusToPasswordLine();
void moveFocusToAcceptButton();
private:
QLineEdit* usernameLine;
QLineEdit* passwordLine;
QPushButton* acceptButton;
QPushButton* cancelButton;
QPushButton* errorAcceptButton;
QDialog* errorWindow;
bool checkAuth();
};
#endif // AUTHDIALOG_HPP
#include "authdialog.hpp"
AuthDialog::AuthDialog(QWidget *parent)
: QDialog(parent) {
//Login window
setWindowTitle(tr("Login"));
usernameLine = new QLineEdit(this);
passwordLine = new QLineEdit(this);
connect(usernameLine, SIGNAL(editingFinished()),
this, SLOT(moveFocusToPasswordLine()));
passwordLine->setEchoMode(QLineEdit::Password);
connect(passwordLine, SIGNAL(editingFinished()),
this, SLOT(moveFocusToAcceptButton()));
QLabel* usernameLabel = new QLabel(tr("Username"), this);
QLabel* passwordLabel = new QLabel(tr("Password"), this);
acceptButton = new QPushButton(tr("Accept"), this);
acceptButton->setAutoDefault(FALSE);
connect(acceptButton, SIGNAL(clicked()),
this, SLOT(onAcceptButtonPressed()));
cancelButton = new QPushButton(tr("Cancel"), this);
cancelButton->setAutoDefault(FALSE);
connect(cancelButton, SIGNAL(clicked()),
this, SLOT(reject()));
QHBoxLayout* buttonsLayout = new QHBoxLayout;
buttonsLayout->setAlignment(Qt::AlignCenter);
buttonsLayout->addWidget(acceptButton);
buttonsLayout->addWidget(cancelButton);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(usernameLabel);
mainLayout->addWidget(usernameLine);
mainLayout->addWidget(passwordLabel);
mainLayout->addWidget(passwordLine);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
//Error window
errorWindow = new QDialog;
errorWindow->setWindowTitle(tr("Error"));
errorAcceptButton = new QPushButton(tr("Ok"), errorWindow);
connect(errorAcceptButton, SIGNAL(clicked()),
this, SLOT(onAcceptErrorButtonPressed()));
QLabel* errorLabel = new QLabel(tr("Incorrect username or password!"), errorWindow);
QVBoxLayout* errorLayout = new QVBoxLayout(errorWindow);
errorLayout->setAlignment(Qt::AlignCenter);
errorLayout->addWidget(errorLabel);
errorLayout->addWidget(errorAcceptButton);
errorWindow->setLayout(errorLayout);
}
AuthDialog::~AuthDialog() {
}
void AuthDialog::onAcceptButtonPressed() {
if (checkAuth()) {
emit accepted();
delete errorWindow;
hide();
}
else {
// deactivate the dialog, reset its fields and show "error message" window
this->setDisabled(TRUE);
errorWindow->show();
usernameLine->clear();
passwordLine->clear();
}
}
bool AuthDialog::checkAuth() {
if ((usernameLine->text() == "username") && (passwordLine->text() == "password")) {
return TRUE;
}
else {
return FALSE;
}
}
void AuthDialog::onAcceptErrorButtonPressed() {
// hide the "error message" window and activate the dialog
errorWindow->hide();
this->setDisabled(FALSE);
usernameLine->setFocus();
acceptButton->setDefault(FALSE);
}
void AuthDialog::moveFocusToPasswordLine() {
// move the focus to the "password" input field when pressed "Enter" key
passwordLine->setFocus();
}
void AuthDialog::moveFocusToAcceptButton() {
// move the focus to the "accept" button and make it active
acceptButton->setFocus();
acceptButton->setDefault(TRUE);
}
#include "authdialog.hpp"
#include <QtCore>
#include <QtGui>
int main(int argc, char** argv) {
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.setWindowTitle("MainWindow");
mainWindow.setMinimumSize(QSize(600, 300));
AuthDialog authDialog;
authDialog.show();
QObject::connect(&authDialog, SIGNAL(accepted()),
&mainWindow, SLOT(show()));
QObject::connect(&authDialog, SIGNAL(rejected()),
&app, SLOT(closeAllWindows()));
return app.exec();
}