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
#include <QDebug>
#include "Application.h"
int main( int argc , char *argv[] ) {
Application myApp(argc , argv);
return myApp.exec();
}
#include <QDebug>
#include "Application.h"
int main( int argc , char *argv[] ) {
Application myApp(argc , argv);
return myApp.exec();
}
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:
#ifndef __APPLICATION_H__
#define __APPLICATION_H__
#include <QFile>
#include <QApplication>
#include <QSessionManager>
Q_OBJECT
private:
public:
Application( int argc , char *argv[] );
public slots:
};
#endif // __APPLICATION_H__
#ifndef __APPLICATION_H__
#define __APPLICATION_H__
#include <QFile>
#include <QApplication>
#include <QSessionManager>
class Application: public QApplication {
Q_OBJECT
private:
QFile lockFile;
public:
Application( int argc , char *argv[] );
void commitData( QSessionManager &manager);
public slots:
void bailout(QSessionManager &manager);
};
#endif // __APPLICATION_H__
To copy to clipboard, switch view to plain text mode
and Application.cpp:
#include <QDebug>
#include <QApplication>
#include <QSessionManager>
#include "Application.h"
Application
::Application( int argc ,
char *argv
[] ): QApplication( argc , argv
) { lockFile.
setFileName( QString( "%1/instance.lock" ).
arg( qApp
->applicationDirPath
() ) );
connect(
this , SIGNAL(commitDataRequest(QSessionManager&))
, this , SLOT(bailout(QSessionManager&))
, Qt::DirectConnection
);
}
void Application::commitData(QSessionManager& manager) {
qDebug() << "Commiting data :-)";
lockFile.close();
lockFile.remove();
manager.release();
}
qDebug() << "I'm bailing out!";
manager.release();
}
#include <QDebug>
#include <QApplication>
#include <QSessionManager>
#include "Application.h"
Application::Application( int argc , char *argv[] ): QApplication( argc , argv ) {
lockFile.setFileName( QString( "%1/instance.lock" ).arg( qApp->applicationDirPath() ) );
lockFile.open(QIODevice::WriteOnly);
connect(
this , SIGNAL(commitDataRequest(QSessionManager&))
, this , SLOT(bailout(QSessionManager&))
, Qt::DirectConnection
);
}
void Application::commitData(QSessionManager& manager) {
qDebug() << "Commiting data :-)";
lockFile.close();
lockFile.remove();
manager.release();
}
void Application::bailout(QSessionManager &manager) {
qDebug() << "I'm bailing out!";
manager.release();
}
To copy to clipboard, switch view to plain text mode
This is the project file, in case it's usefull...
TEMPLATE = app
SOURCES += main.cpp Application.cpp
CONFIG += console warn_on
HEADERS += Application.h
TEMPLATE = app
SOURCES += main.cpp Application.cpp
CONFIG += console warn_on
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
Bookmarks