Results 1 to 8 of 8

Thread: commitData doesn't seem to be called...

  1. #1
    Join Date
    Sep 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default commitData doesn't seem to be called...

    Hello everyone.
    This is my first post here and I'm fairly new to C++ and Qt programming.
    Here's my problem.
    I'm working on an application written in Qt4.4.0 in MS windows.
    The application allows only one instance and uses a file that, if present, means an instance is running. So, I create this file on application startup and delete it on application exit.
    This works fine when the program ends by user interaction but it's not working when I close a windows session, or by a windows shutdown.

    I made a simple testcase to illustrate my problem, so that any willing soul can give it a shot and point me some sort of error that I'm making here....

    This is main.cpp
    Qt Code:
    1. #include <QDebug>
    2. #include "Application.h"
    3.  
    4. int main( int argc , char *argv[] ) {
    5. Application myApp(argc , argv);
    6.  
    7. return myApp.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    and here's my app class, subclassed from QApplication, so that I could reimplement commitData():
    Application.h:
    Qt Code:
    1. #ifndef __APPLICATION_H__
    2. #define __APPLICATION_H__
    3. #include <QFile>
    4. #include <QApplication>
    5. #include <QSessionManager>
    6.  
    7. class Application: public QApplication {
    8. Q_OBJECT
    9. private:
    10. QFile lockFile;
    11. public:
    12. Application( int argc , char *argv[] );
    13. void commitData( QSessionManager &manager);
    14.  
    15. public slots:
    16. void bailout(QSessionManager &manager);
    17. };
    18.  
    19. #endif // __APPLICATION_H__
    To copy to clipboard, switch view to plain text mode 

    and Application.cpp:
    Qt Code:
    1. #include <QDebug>
    2. #include <QApplication>
    3. #include <QSessionManager>
    4. #include "Application.h"
    5.  
    6. Application::Application( int argc , char *argv[] ): QApplication( argc , argv ) {
    7. lockFile.setFileName( QString( "%1/instance.lock" ).arg( qApp->applicationDirPath() ) );
    8. lockFile.open(QIODevice::WriteOnly);
    9.  
    10. connect(
    11. this , SIGNAL(commitDataRequest(QSessionManager&))
    12. , this , SLOT(bailout(QSessionManager&))
    13. , Qt::DirectConnection
    14. );
    15. }
    16.  
    17. void Application::commitData(QSessionManager& manager) {
    18. qDebug() << "Commiting data :-)";
    19. lockFile.close();
    20. lockFile.remove();
    21. manager.release();
    22. }
    23.  
    24. void Application::bailout(QSessionManager &manager) {
    25. qDebug() << "I'm bailing out!";
    26. manager.release();
    27. }
    To copy to clipboard, switch view to plain text mode 

    This is the project file, in case it's usefull...
    Qt Code:
    1. TEMPLATE = app
    2. SOURCES += main.cpp Application.cpp
    3. CONFIG += console warn_on
    4. HEADERS += Application.h
    To copy to clipboard, switch view to plain text mode 

    I'm using Qt 4.4.0 on windows with Mingw gcc 3.4.5
    Altough I (think that I) reimplemented commitData() as mentioned in the docs, it seems to never be called.
    I launched the program in a cmd.exe terminal, and also from the explorer shell, then I close my session, I see the application finishes ( terminal returns to prompt ), but no messages are printed, and the lock file isn't deleted at all. Tested with closeing session and with windows shutdown.

    What am I doing wrong here? I suppose it must be some silly mistake, but I'm not experienced enough to detect it. Is commitData() actually reimplemented correctly, or is it not being called at all??

    Any help would be very, very apreciated at this point...

    cheers,
    --to
    Last edited by tone; 8th September 2008 at 16:40. Reason: typos :-)

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: commitData doesn't seem to be called...

    your commitData is just a regular public function but not a signal. And that your connect doesn't do what you think, because in that connect you treat commitData as a signal when it is not.

    Before moving further we need to know, when do you want commitData to be called?
    Last edited by lyuts; 8th September 2008 at 20:21. Reason: updated contents
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Sep 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: commitData doesn't seem to be called...

    Well.. yes... nothing does what I think it does...
    From what I read, commitData() is invoqued when the session is closingn and the application needs to save some stuff, ( eg: save an open file in an editor).

    These are the docs from which I've been basing on:
    http://doc.trolltech.com/4.4/qapplic...tml#commitData
    http://doc.trolltech.com/4.4/session.html (see "Getting Session Management to Work with Qt" )

    I didn't read anywhere that it needs to be a slot, all I read is that it can be reimplemented to handle the graceful logout or even abort it.

    I want my application to detect when it's about to close due to a logout, clean it's internal state normally (without killing timers and thread by brute force) and remove the lock file so that I don't detect a false instance of the application when it starts the next time.

    cheers,
    --to

    NB: I could just handle the application instance using a lock on the file, and checking if I can create/open it to decide if some other instance is using it. But, since this session management thing is documented this way, it seems a more reasonable solution, and I need it to shut down my app gracefully without simply exploding its internals.

  4. #4
    Join Date
    Sep 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: commitData doesn't seem to be called...

    Quote Originally Posted by lyuts View Post
    your commitData is just a regular public function but not a signal. And that your connect doesn't do what you think, because in that connect you treat commitData as a signal when it is not.
    Please have a second look... What I am connecting is the commitDataRequest() *signal*, which is emmited when a logout hapens, according to the docs. This code compiles and executes, and the runtime doesn't throw any complaints about non-existant signals and/or slots...

    This signal is inherited from QApplication.
    the commitData() [virtual] method is also inherited, but I'm reimplementing it (or trying to) to catch the logout process and do some more internal work before bailing out. It has, however a default implementation, and I wonder if I'm not correctly (or at all) overriding this default implementation with the one I'm providing in my class...

    commitData is a virtual method from QApplication, whose default implementation sends close events to all open windows of the application and lets the logout process continue if all windows accept the event.
    Maybe I'm just mis-interpretating the docs, I don't know,and because I'm new to Qt, this might be all wrong...

    cheers,
    --to

  5. #5
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: commitData doesn't seem to be called...

    Yep, that's my fault, i thought you were connecting commitData. One of another solutions is to implement a slot which will commit your data, and connect it to commitDataRequest.
    I'm a rebel in the S.D.G.

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: commitData doesn't seem to be called...

    Try adding
    CONFIG += console warn_on debug
    to pro file and run again your first version. What is the output?
    I'm a rebel in the S.D.G.

  7. #7
    Join Date
    Sep 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: commitData doesn't seem to be called...

    Quote Originally Posted by lyuts View Post
    One of another solutions is to implement a slot which will commit your data, and connect it to commitDataRequest.
    That slot is bailout(...). The connection is made but bailout seems not to be called as well

    Quote Originally Posted by lyuts View Post
    Try adding
    CONFIG += console warn_on debug
    to pro file and run again your first version. What is the output?
    I'll try it out tomorrow. It's late here, and I'm not near the machine where I'm trying this stuff...

    Does anyone have experience handling the session logout in Qt under Windows XP/Vista? Looking at the docs this seems to be a fairly trivial matter, but I can't get my code to work. Surely there must be some silly mistake that I need to find...

    cheers,
    --to

  8. #8
    Join Date
    Sep 2008
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: commitData doesn't seem to be called...

    Ok, I found the silly mistake...actually, someone pointed it to me.
    The problem is the 'console' keyword in the CONFIG variable of the project file. It causes the session management mechanism to work in some diferent way, or not to work at all.
    Removing the 'console' option, everything orks just fine. The code that I used to test it is already diferent from the one posted here, but I think this one will work equally just fine.

    cheers,
    --to

Similar Threads

  1. setSceneRect not being called properly?
    By bjh in forum Qt Programming
    Replies: 7
    Last Post: 12th July 2009, 19:42
  2. Replies: 1
    Last Post: 7th August 2008, 13:46
  3. QWidget::resizeEvent is not called
    By maximAL in forum Qt Programming
    Replies: 9
    Last Post: 3rd February 2008, 16:41
  4. Replies: 3
    Last Post: 20th February 2007, 13:02
  5. QWidget::paintEngine() should no longer be called
    By Greeny in forum Qt Programming
    Replies: 7
    Last Post: 7th February 2007, 10:12

Tags for this Thread

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.