Below is main.cpp:
#include <QApplication>
#include "homestead.h"
#include "dlglogin.h"
#define DBDRIVER "QPSQL"
#define DBHOST "localhost"
#define DBNAME "hap2008"
int main( int argc, char * argv[] ) {
app.setQuitOnLastWindowClosed(false); // may not be needed any more
dlgLogin dlg;
if( dlg.
exec() == QDialog::Accepted ){ hapdb.setHostName(DBHOST);
hapdb.setDatabaseName(DBNAME);
hapdb.setUserName(dlg.dui.leUserName->text());
hapdb.setPassword(dlg.dui.leUserPassword->text());
if ( hapdb.open() ) {
homestead ht;
ht.RevID = dlg.dui.leUserName->text();
ht.show();
app.setQuitOnLastWindowClosed(true); // may not be needed any more
return app.exec();
} else {
strRejected
= QString("Reason: %1").
arg(hapdb.
lastError().
text()).
toLatin1();
QMessageBox::information(0,
"Login Rejected!",strRejected,
return 1;
}
} else {
strRejected
= QString("User Canceled the login!").
toLatin1();
QMessageBox::information(0,
"Login Canceled!",strRejected,
return 2;
}
}
#include <QApplication>
#include "homestead.h"
#include "dlglogin.h"
#define DBDRIVER "QPSQL"
#define DBHOST "localhost"
#define DBNAME "hap2008"
int main( int argc, char * argv[] ) {
QString strRejected = "";
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false); // may not be needed any more
dlgLogin dlg;
if( dlg.exec() == QDialog::Accepted ){
QSqlDatabase hapdb = QSqlDatabase::addDatabase(DBDRIVER);
hapdb.setHostName(DBHOST);
hapdb.setDatabaseName(DBNAME);
hapdb.setUserName(dlg.dui.leUserName->text());
hapdb.setPassword(dlg.dui.leUserPassword->text());
if ( hapdb.open() ) {
homestead ht;
ht.RevID = dlg.dui.leUserName->text();
ht.show();
app.setQuitOnLastWindowClosed(true); // may not be needed any more
return app.exec();
} else {
strRejected = QString("Reason: %1").arg(hapdb.lastError().text()).toLatin1();
QMessageBox::information(0,"Login Rejected!",strRejected,
QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
return 1;
}
} else {
strRejected = QString("User Canceled the login!").toLatin1();
QMessageBox::information(0,"Login Canceled!",strRejected,
QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
return 2;
}
}
To copy to clipboard, switch view to plain text mode
Below is file dlglogin.h:
#ifndef DLGLOGIN_H
#define DLGLOGIN_H
#include "ui_dlglogin.h"
{
Q_OBJECT
public:
Ui::dlgLoginUi dui;
dlgLogin()
{
// Only ID and password required in UI. No cpp file needed.
dui.setupUi(this);
dui.leUserName->setText("");
dui.leUserPassword->setText("");
dui.leUserName->setFocus();
dui.leUserName->selectAll();
}
};
#endif
#ifndef DLGLOGIN_H
#define DLGLOGIN_H
#include "ui_dlglogin.h"
class dlgLogin : public QDialog
{
Q_OBJECT
public:
Ui::dlgLoginUi dui;
dlgLogin()
{
// Only ID and password required in UI. No cpp file needed.
dui.setupUi(this);
dui.leUserName->setText("");
dui.leUserPassword->setText("");
dui.leUserName->setFocus();
dui.leUserName->selectAll();
}
};
#endif
To copy to clipboard, switch view to plain text mode
In dlglogin.ui form file the form object is called "dlgLoginUi" and contains the name and password as textboxes. The cancel button is connected to the reject() function and the OK button is connected to the accept() function.
There is more explainations in the forum tutorial
http://wiki.qtcentre.org/index.php?title=Programming_in_Qt4_and_C%2B%2B
Bookmarks