Results 1 to 13 of 13

Thread: Trouble with app logoff

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Trouble with app logoff

    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:
    Qt Code:
    1. ...
    2. #include "TraderProc.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. TraderProc *trProc = new TraderProc;
    8. trProc->startLogin();
    9. return app.exec();
    10. delete trProc;
    11. }
    To copy to clipboard, switch view to plain text mode 

    In TraderProc.cpp:

    Qt Code:
    1. ...
    2. #include "Login.h"
    3. #include "MainWindow.h"
    4. ...
    5.  
    6. void TraderProc::startLogin()
    7. {
    8. Login login;
    9. int l = login.exec();
    10.  
    11. if (l == QDialog::Accepted)
    12. isLogin = true;
    13. else
    14. isLogin = false;
    15.  
    16. this->openTrader(isLogin);
    17. }
    18.  
    19. void TraderProc::openTrader(bool log)
    20. {
    21. if (log)
    22. {
    23. MainWindow *mainWin = new MainWindow;
    24. mainWin->show();
    25. }
    26. else
    27. {
    28. exit(0);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    In MainWindow:

    Qt Code:
    1. ...
    2. #include "TraderProc.h"
    3. ...
    4.  
    5. void MainWindow::Logoff()
    6. {
    7. TraderProc *trProc = new TraderProc;
    8. this->close();
    9. trProc->startLogin();
    10. }
    To copy to clipboard, switch view to plain text mode 

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

    Marcelo E. Geyer
    Brazil

  2. #2
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with app logoff

    hehe, what are you doing here? delete after return???
    Qt Code:
    1. return app.exec();
    2. delete trProc;
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Trouble with app logoff

    To solve the event loop issue, make startLogin() a slot and use QTimer::singleShot()
    Qt Code:
    1. #include "TraderProc.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6. TraderProc trProc;
    7. QTimer::singleShot(0, &trProc, SLOT(startLogin()));
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    And do the same at logoff. You should probably make TraderProc a singleton. No need to create a new one every time.
    Qt Code:
    1. #include "TraderProc.h"
    2. ...
    3.  
    4. void MainWindow::Logoff()
    5. {
    6. close();
    7. QTimer::singleShot(0, globalTraderProc(), SLOT(startLogin()));
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Trouble with app logoff

    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:

    Qt Code:
    1. #include <QApplication>
    2. #include <QCoreApplication>
    3. #include "TraderProc.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. Q_INIT_RESOURCE(trader);
    8. QT_REQUIRE_VERSION(argc, argv, "4.3.4");
    9.  
    10. QApplication app(argc, argv);
    11.  
    12. QTimer::singleShot(0, TraderProc::TrProc(), SLOT(startLogin()));
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    In TraderProc.h:

    Qt Code:
    1. #include <QObject>
    2. #include <QCoreApplication>
    3. #include <QTimer>
    4. #include "engine/Login.h"
    5. #include "MainWindow.h"
    6.  
    7. class Login;
    8. class MainWindow;
    9. class TraderProc: public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. static TraderProc *TrProc();
    14.  
    15. private:
    16. TraderProc();
    17. static TraderProc *_TrProc;
    18. bool isLogin;
    19. void openTrader(bool );
    20.  
    21. public slots:
    22. void startLogin();
    23.  
    24. protected:
    25.  
    26. };
    27. #endif
    To copy to clipboard, switch view to plain text mode 

    In TraderProc.cpp

    Qt Code:
    1. #include "TraderProc.h"
    2.  
    3. TraderProc *TraderProc::_TrProc = 0;
    4.  
    5. TraderProc *TraderProc::TrProc()
    6. {
    7. if (_TrProc == 0)
    8. _TrProc = new TraderProc;
    9.  
    10. return _TrProc;
    11. }
    12.  
    13. TraderProc::TraderProc()
    14. :isLogin(false)
    15. {
    16.  
    17. }
    18.  
    19. void TraderProc::startLogin()
    20. {
    21. Login login;
    22. int l = login.exec();
    23.  
    24. if (l == QDialog::Accepted)
    25. {
    26. isLogin = true;
    27. }
    28. else
    29. {
    30. isLogin = false;
    31. }
    32.  
    33. openTrader(isLogin);
    34. }
    35.  
    36. void TraderProc::openTrader(bool log)
    37. {
    38. if (log)
    39. {
    40. MainWindow *mainWin = new MainWindow;
    41. mainWin->show();
    42. }
    43. else
    44. {
    45. exit(0);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    In MainWindow.cpp

    Qt Code:
    1. ...
    2.  
    3. void MainWindow::logoff()
    4. {
    5. close();
    6. QTimer::singleShot(0, TraderProc::TrProc(), SLOT(startLogin()));
    7. }
    8.  
    9. ...
    To copy to clipboard, switch view to plain text mode 


    Thanks for help.
    Marcelo E. Geyer
    Attached Images Attached Images

  5. #5
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with app logoff

    Check if following works or not
    Qt Code:
    1. qApp->setQuitOnLastWindowClosed(false);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with app logoff

    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?

  7. #7
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with app logoff

    void QCoreApplication::quit ()

  8. #8
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Trouble with app logoff

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with app logoff

    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?

  10. #10
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with app logoff

    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.

  11. #11
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with app logoff

    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
    Qt Code:
    1. ...
    2. #include "Db.h"
    3.  
    4. class Login : public QDialog, public DB
    5. {
    6. Q_OBJECT
    7. public:
    8. Login(QWidget *parent = 0);
    9. virtual ~Login();
    10.  
    11. private:
    12. Ui::Login ui;
    13. void AuthUser(); // Autentica o usuário
    14. bool isLoginOK();
    15.  
    16. private slots:
    17. protected:
    18. void keyPressEvent(QKeyEvent *e); // Reimplementa KeyPressEvent
    19. };
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    In Login.cpp
    Qt Code:
    1. #include "Login.h"
    2.  
    3. Login::Login(QWidget *parent)
    4. :QDialog(parent), errorLogin(false)
    5. {
    6. ui.setupUi(this);
    7. ...
    8. }
    9.  
    10. Login::~Login()
    11. {
    12.  
    13. }
    14.  
    15. void Login::keyPressEvent(QKeyEvent *e)
    16. {
    17. switch (e->key())
    18. {
    19. case Qt::Key_Return:
    20. AuthUser();
    21. break;
    22. case Qt::Key_Escape:
    23. reject();
    24. break;
    25. default:
    26. break;
    27. }
    28. }
    29.  
    30. bool Login::isLoginOK()
    31. {
    32. if (errorLogin == false)
    33. {
    34. return true;
    35. }
    36. else
    37. {
    38. return false;
    39. }
    40. }
    41.  
    42. void Login::AuthUser()
    43. {
    44. DB::createConnection();
    45.  
    46. // for only this test
    47. accept();
    48. }
    49.  
    50. ...
    To copy to clipboard, switch view to plain text mode 

    Thanks for helping,
    Marcelo E. Geyer
    Attached Files Attached Files

  12. #12
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with app logoff

    ops, and Db.h:
    Qt Code:
    1. ...
    2. class DB
    3. {
    4. public:
    5. DB();
    6. virtual ~DB();
    7.  
    8. static bool createConnection();
    9.  
    10. private:
    11.  
    12. protected:
    13. };
    14. #endif
    To copy to clipboard, switch view to plain text mode 

    In Db.cpp
    Qt Code:
    1. #include "Db.h"
    2.  
    3. DB::DB()
    4. {
    5.  
    6. }
    7.  
    8. DB::~DB()
    9. {
    10.  
    11. }
    12.  
    13. bool DB::createConnection()
    14. {
    15. QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE");
    16. db.setHostName("127.0.0.1");
    17. db.setDatabaseName("/opt/Trader/Database/devel.fdb");
    18. db.setUserName("SYSDBA");
    19. db.setPassword("masterkey");
    20.  
    21. if (!db.open())
    22. {
    23. QMessageBox::critical(0, qApp->tr("Erro de conexão"),
    24. qApp->tr("Não foi possÃ*vel estabelecer uma conexão com o \n"
    25. "banco de dados. Contate o suporte técnico."),
    26.  
    27. return false;
    28. }
    29. else
    30. {
    31. qDebug() << "Connected!";
    32. return true;
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,

    Marcelo

  13. #13
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: [SOLVED] Trouble with app logoff

    Good day,

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

    In Db.h
    Qt Code:
    1. ...
    2. class DB
    3. {
    4. public:
    5. DB();
    6. virtual ~DB();
    7.  
    8. static bool createConnection();
    9. static QSqlDatabase db;
    10.  
    11. private:
    12.  
    13. protected:
    14. };
    15. #endif
    To copy to clipboard, switch view to plain text mode 

    In Db.cpp
    Qt Code:
    1. #include "Db.h"
    2.  
    3. QSqlDatabase DB::db;
    4.  
    5. DB::DB()
    6. {
    7.  
    8. }
    9.  
    10. DB::~DB()
    11. {
    12. qDebug() << "removendo database";
    13. }
    14.  
    15. bool DB::createConnection()
    16. {
    17. db = QSqlDatabase::addDatabase("QIBASE");
    18. db.setHostName("127.0.0.1");
    19. db.setDatabaseName("/opt/Trader/Database/devel.fdb");
    20. db.setUserName("SYSDBA");
    21. db.setPassword("masterkey");
    22.  
    23. if (!db.open())
    24. {
    25. QMessageBox::critical(0, qApp->tr("Erro de conexão"),
    26. qApp->tr("Não foi possÃ*vel estabelecer uma conexão com o \n"
    27. "banco de dados. Contate o suporte técnico."),
    28.  
    29. return false;
    30. }
    31. else
    32. {
    33. qDebug() << "Connected!";
    34. return true;
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    In Login.cpp
    Qt Code:
    1. ...
    2. void Login::keyPressEvent(QKeyEvent *e)
    3. {
    4. switch (e->key())
    5. {
    6. case Qt::Key_Return:
    7. AuthUser();
    8. break;
    9. case Qt::Key_Escape:
    10. qDebug() << "Quiting...";
    11. if (db.isOpen())
    12. db.close();
    13. reject();
    14. break;
    15. default:
    16. break;
    17. }
    18. }
    19. ...
    To copy to clipboard, switch view to plain text mode 

    Thanks for all cooperate!
    Marcelo.

Similar Threads

  1. Replies: 9
    Last Post: 12th July 2011, 22:14
  2. Replies: 3
    Last Post: 19th November 2007, 15:31
  3. Trouble placing QLabel on top ofQGLWidget
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 8th October 2007, 23:48
  4. Replies: 18
    Last Post: 6th February 2007, 14:06
  5. Memory Trouble
    By nimrod in forum Newbie
    Replies: 4
    Last Post: 6th February 2006, 18:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.