PDA

View Full Version : How to not open the application second time if it is already running?



sudheer168
22nd November 2008, 12:55
Hi ,I am using QT4.4 open source ,I have problem that If I click on my application second time, I get two same applications running at the same time. I tried this as shown below .



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile lockFile("E:\\lockFile.lock");
if (lockFile.isOpen())
{
QMessageBox::about(0, "Attention", "The app is already running");
exit(1);
}
lockFile.open(QFile::WriteOnly);
lpsu_application lp;
lp.show();
return a.exec();
}



But it is not working.I think the first application creates the file but it does not "hold" it and the next one does not see that the file is open.
So help me to solve this problem with out using QtSingleApplication() class.

With Regards,
Sudheeer

pastor
22nd November 2008, 14:06
You can use followng for that:

QtSingleApplication (http://trolltech.com/products/appdev/add-on-products/catalog/4/Utilities/qtsingleapplication/) //Commercial
SingleApplication (http://qt-apps.org/content/show.php/SingleApplication?content=81163) //LGPL

sudheer168
24th November 2008, 07:03
Hi ,thanks for the quick reply.I have opened the link SingleApplication and dowloaded the total file.I go through it and find that it is useful to me.But one thing ,I don't no how to integrate this class with QT.
So help me or suggest me to solve the problem.

With Regards,

Sudheer.

Lesiok
24th November 2008, 08:30
To solve look at examples in SingleApplication package :

QApplication app(argc, argv);
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
// below is the magic code
SingleApplication instance("TrivialExample", &app);
if(instance.isRunning())
{
QString message = "Hello! Did You hear other Trivial Example instance? :)\n"
"PID: " + QString::number(app.applicationPid());
if(instance.sendMessage(message))
return 0;
}

sudheer168
24th November 2008, 09:59
HI thanks for your kind reply.Actually my question is about how to integrate SingleApplication
package with my QT4.4.0.
So hlep me to solve this.

With Regards,
Sudheer.

pastor
24th November 2008, 10:28
What's problem do you have with SingleApplication and QT4.4.0? Build errors or so?

Lesiok
24th November 2008, 12:02
Actually my question is about how to integrate SingleApplication
package with my QT4.4.0.
You must build library with SingleApplication package and link them with in Your's project. All is described in SingleApplication package docs.

sudheer168
24th November 2008, 12:22
Hi Lesiok,thanks for your kind reply.I will refer the docs and try it.

Thanks and With Regards,
Sudheer.