If I click on my application second time, I get two same applications running at the same time. Is there any way to for an application to check if it is running previously and do not start if it so?
If I click on my application second time, I get two same applications running at the same time. Is there any way to for an application to check if it is running previously and do not start if it so?
This has been asked many times before.
Please search the forum first before posting questions.
This thread might be of interest to you:
http://www.qtcentre.org/forum/f-qt-p...gleApplication
I didn't find any real solution... thanks anyway
Really? Did you try searching for "QtSingleApplication"?
slava (9th March 2008)
I looked through all that. I like the idea of "lockFile" but somehow on my WinXP planform I do:
Qt Code:
{ ... } ...To copy to clipboard, switch view to plain text mode
The first application creates the file but it does not "hold" it and the next one does not see that the file is open. I can delete or edit the file without any notification that it is used by something else.
Last edited by jpn; 9th March 2008 at 17:46. Reason: missing [code] tags
That's because you create the QFile object as a temporary variable so it gets destroyed immediately effectively closing the file and removing the lock.
slava (9th March 2008)
Sorry, how do I do that, put
private:
QFile lockFile;
in the header file or I'm completely missing it
Thank you I have found it. I just put it before the window::window() part. That is working perfectly now. Thank you very much!
.... just a last question. When I do:
Qt Code:
{ close(); } ...To copy to clipboard, switch view to plain text mode
because of this "close();" the application crushes. It is good that it does not start second time but it produces a windows error saying that "application suddenly terminated, let's report the bug through the internet" or something. I just push ingnore and that is fine but can I do it without that terrible message from Windows?
Last edited by jpn; 10th March 2008 at 12:30. Reason: missing [code] tags
Try to wrap the stuff after the if () {} block into an else block so it is not executed at all in case the if clause is true. Maybe the stuff is executed even after close() since close() does not automatically mean an immediate exit/deletion; Also try to set the Qt::WA_DeleteOnClose attribute for your widget.
It's also a good Idea to check if the file could be successfully opened and to return if not:
Qt Code:
return; }To copy to clipboard, switch view to plain text mode
Last edited by momesana; 10th March 2008 at 10:43.
slava (10th March 2008)
Thank you for the ideas. It is working now, I moved those lines to main.cpp just before the whole thing stats so it checks it earlier and returns if application is already running and that worked fine. Thank you.
Bookmarks