PDA

View Full Version : Trouble with app logoff



estanisgeyer
24th March 2008, 12:24
Good Day,

I have a function to implement a logoff in my system, but the way I am making the application is hung. I will post here the code:

main.cpp:


...
#include "TraderProc.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TraderProc *trProc = new TraderProc;
trProc->startLogin();
return app.exec();
delete trProc;
}


In TraderProc.cpp:



...
#include "Login.h"
#include "MainWindow.h"
...

void TraderProc::startLogin()
{
Login login;
int l = login.exec();

if (l == QDialog::Accepted)
isLogin = true;
else
isLogin = false;

this->openTrader(isLogin);
}

void TraderProc::openTrader(bool log)
{
if (log)
{
MainWindow *mainWin = new MainWindow;
mainWin->show();
}
else
{
exit(0);
}
}



In MainWindow:




...
#include "TraderProc.h"
...

void MainWindow::Logoff()
{
TraderProc *trProc = new TraderProc;
this->close();
trProc->startLogin();
}



What would be the best way to take out the application, automatically restarting. Suggestions are welcome.
Thanks,

Marcelo E. Geyer
Brazil

yogeshm02
24th March 2008, 13:46
hehe, what are you doing here? delete after return???


return app.exec();
delete trProc;


If Login is a dialog, then the problem could be that you are creating an event loop even before main event loop is executing. I've not come across this kind of problem so can't be sure.

spud
24th March 2008, 16:54
To solve the event loop issue, make startLogin() a slot and use QTimer::singleShot()



#include "TraderProc.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TraderProc trProc;
QTimer::singleShot(0, &trProc, SLOT(startLogin()));
return app.exec();
}

And do the same at logoff. You should probably make TraderProc a singleton. No need to create a new one every time.

#include "TraderProc.h"
...

void MainWindow::Logoff()
{
close();
QTimer::singleShot(0, globalTraderProc(), SLOT(startLogin()));
}

estanisgeyer
24th March 2008, 19:04
Hi guys,

Using singleton was a good idea, I use in class TraderProc and the result is we still have application locked, see what happens when I logoff, the application hangs. Below the code:

In main.cpp:



#include <QApplication>
#include <QCoreApplication>
#include "TraderProc.h"

int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(trader);
QT_REQUIRE_VERSION(argc, argv, "4.3.4");

QApplication app(argc, argv);

QTimer::singleShot(0, TraderProc::TrProc(), SLOT(startLogin()));
return app.exec();
}


In TraderProc.h:



#include <QObject>
#include <QCoreApplication>
#include <QTimer>
#include "engine/Login.h"
#include "MainWindow.h"

class Login;
class MainWindow;
class TraderProc: public QObject
{
Q_OBJECT
public:
static TraderProc *TrProc();

private:
TraderProc();
static TraderProc *_TrProc;
bool isLogin;
void openTrader(bool );

public slots:
void startLogin();

protected:

};
#endif


In TraderProc.cpp



#include "TraderProc.h"

TraderProc *TraderProc::_TrProc = 0;

TraderProc *TraderProc::TrProc()
{
if (_TrProc == 0)
_TrProc = new TraderProc;

return _TrProc;
}

TraderProc::TraderProc()
:isLogin(false)
{

}

void TraderProc::startLogin()
{
Login login;
int l = login.exec();

if (l == QDialog::Accepted)
{
isLogin = true;
}
else
{
isLogin = false;
}

openTrader(isLogin);
}

void TraderProc::openTrader(bool log)
{
if (log)
{
MainWindow *mainWin = new MainWindow;
mainWin->show();
}
else
{
exit(0);
}
}



In MainWindow.cpp



...

void MainWindow::logoff()
{
close();
QTimer::singleShot(0, TraderProc::TrProc(), SLOT(startLogin()));
}

...



Thanks for help.
Marcelo E. Geyer

yogeshm02
25th March 2008, 05:30
Check if following works or not

qApp->setQuitOnLastWindowClosed(false);

estanisgeyer
25th March 2008, 13:36
Hi,

Sure, partly solved my problem, but now I go back to the early stage of this post. When closing the window to Login after making the out, the application is still running. I used this method setQuitOnLastWindowClosed (true) in the method startLogin () to close any window that the application end, but not worked. Any ideas?

yogeshm02
25th March 2008, 13:41
void QCoreApplication::quit ()

estanisgeyer
25th March 2008, 14:12
I have in my dialogue reimplementation of the login method keyPressEvent includes your suggestion at the event for the ESC key, but still the same. But if you look at the method openTrader () I already posted above, you will see that I exit (0) - Trying with qApp->quit() also, but did not work ... Still hanging.

Thanks for helping.

Marcelo.

yogeshm02
25th March 2008, 14:26
What about other parts of you code; if your project *real* or you are just trying out things and if its small enough can you share it here with us?

estanisgeyer
25th March 2008, 15:07
It is a real project. I just making a minimum code involving these classes to post here and just working. I will have to check what is happening that the application is getting hung.

Thanks,

Marcelo.

estanisgeyer
26th March 2008, 13:41
Good day,

I discovered that my application is hanging because of the connection to the database Interbase / firebird. I am posting a small example that can be compiled, but attention to the information from the database to be exchanged in the spring. They can create a database empty, simply opening Connection is already enough to occur the problem, which still can not solve. I tried to do with the driver QSQLITE, but this does not happen with the problem ... Very strange.

Finally, I will post here the code of connection that is the basis for the class Login:

In Login.h


...
#include "Db.h"

class Login : public QDialog, public DB
{
Q_OBJECT
public:
Login(QWidget *parent = 0);
virtual ~Login();

private:
Ui::Login ui;
void AuthUser(); // Autentica o usuário
bool isLoginOK();

private slots:
protected:
void keyPressEvent(QKeyEvent *e); // Reimplementa KeyPressEvent
};
#endif



In Login.cpp


#include "Login.h"

Login::Login(QWidget *parent)
:QDialog(parent), errorLogin(false)
{
ui.setupUi(this);
...
}

Login::~Login()
{

}

void Login::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
case Qt::Key_Return:
AuthUser();
break;
case Qt::Key_Escape:
reject();
break;
default:
break;
}
}

bool Login::isLoginOK()
{
if (errorLogin == false)
{
return true;
}
else
{
return false;
}
}

void Login::AuthUser()
{
DB::createConnection();

// for only this test
accept();
}

...


Thanks for helping,
Marcelo E. Geyer

estanisgeyer
26th March 2008, 13:45
ops, and Db.h:


...
class DB
{
public:
DB();
virtual ~DB();

static bool createConnection();
QSqlDatabase db;

private:

protected:
};
#endif




In Db.cpp


#include "Db.h"

DB::DB()
{

}

DB::~DB()
{

}

bool DB::createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE");
db.setHostName("127.0.0.1");
db.setDatabaseName("/opt/Trader/Database/devel.fdb");
db.setUserName("SYSDBA");
db.setPassword("masterkey");

if (!db.open())
{
QMessageBox::critical(0, qApp->tr("Erro de conexão"),
qApp->tr("Não foi possÃ*vel estabelecer uma conexão com o \n"
"banco de dados. Contate o suporte técnico."),
QMessageBox::Ok);

return false;
}
else
{
qDebug() << "Connected!";
return true;
}
}




Thanks,

Marcelo

estanisgeyer
27th March 2008, 17:50
Good day,

I solved my problem, making static object db for class QSqlDatabase and call db.close() in Login::keyPressEvent, follow:

In Db.h


...
class DB
{
public:
DB();
virtual ~DB();

static bool createConnection();
static QSqlDatabase db;

private:

protected:
};
#endif



In Db.cpp


#include "Db.h"

QSqlDatabase DB::db;

DB::DB()
{

}

DB::~DB()
{
qDebug() << "removendo database";
}

bool DB::createConnection()
{
db = QSqlDatabase::addDatabase("QIBASE");
db.setHostName("127.0.0.1");
db.setDatabaseName("/opt/Trader/Database/devel.fdb");
db.setUserName("SYSDBA");
db.setPassword("masterkey");

if (!db.open())
{
QMessageBox::critical(0, qApp->tr("Erro de conexão"),
qApp->tr("Não foi possÃ*vel estabelecer uma conexão com o \n"
"banco de dados. Contate o suporte técnico."),
QMessageBox::Ok);

return false;
}
else
{
qDebug() << "Connected!";
return true;
}
}



In Login.cpp


...
void Login::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
case Qt::Key_Return:
AuthUser();
break;
case Qt::Key_Escape:
qDebug() << "Quiting...";
if (db.isOpen())
db.close();
reject();
break;
default:
break;
}
}
...


Thanks for all cooperate!
Marcelo.