Application does not start properly when called on Windows startup
-
I have a strange problem.
I have an Qt application with dynamic linked dll's.
When I open the .exe file the application starts correctly, even if I open it from a desktop shortcut as soon, as I see the desktop on Windows start.
But when I add it to the HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run it does not start correctly.
e.g. (edit) the log file is not created and the QlistView doesnt display any information (list of items) from the SQLite database, and I cannot connect to a server via QTcpSocket. (even if the autostart version starts after the manually started).
(edit) the log file doesn't seem to work as well
SOLUTION:
The solution was to change the current directory to the app directory by:
QDir::setCurrent(QCoreApplication::applicationDirPath());
At startup the current directory is set to windows/system32.
-
Hi,
Out of curiosity, what exactly was failing ?
From what you wrote it seems that you were trying to open your SQLite database relative to your executable location. Is that the case ?
-
@michalos Weird. Unless you were trying to open files or write files in your application's directory then setting the current directory shouldn't have mattered at all. Starting in a different directory shouldn't affect anything.
I'm betting you were trying to use files that you expected in your application's directory and expected it to be the current directory. Which as you learned is not always the case. In fact this is something that gets people a lot, and not just in startup scenarios.
Definitely best to never assume your current working directory is your application start directory. So where you read/write files to your app directory use the full path with something like
QString("%1/myfilename).arg(QCoreApplication::applicationDirPath())
. This way you never assume your directory. System file dialogs and lots of other things can change your current directory while your app is running as well. -
@SGaist :
You are correct. I was opening it relatively.
I'm not sure how could I open it differently.
My code:if(QSqlDatabase::contains("messengerDB")) { _messengerDB = QSqlDatabase::database("messengerDB"); qDebug() << "Database already exists, connecting to DB"; emit databaseConnected(); } else { _messengerDB = QSqlDatabase::addDatabase("QSQLITE", "messengerDB"); _messengerDB.setDatabaseName("memory.db"); if(!_messengerDB.open()) { qDebug()<<"Cannot open database" "Unable to establish a database connection.\n"; emit errorWhileConnecting(); } qDebug() << "Connected to database messengerDB."; emit databaseConnected(); }
@ambershark :
Yes, that was the case.
Thank You for Your advice. -
Use QStandardPaths using
QStandardPaths::AppDataLocation
to retrieve a location that can be written to by your application for your application specific data.This way your application can run on all platforms it will be installed on.
It's usually bad practice to use relative path like that especially when writing files on your application user machines. On all OSs your application usually goes in a central read-only place to 1) avoid funky thing to happen and 2) to avoid having several users accessing data from each other.
-
@SGaist Thank You for Your reply. I didn't think anout that.
In my question I wanted to ask, how to check if my database contains my database messengerDB, when I do not have the location yet.
But I understand the code now. (I wrote it couple of months ago)
I first check if I havent initializie the connection, and if not, then I create a new database, with a specific databaseName, which can contain the whole directory for the database file.
-
What do you mean by "when I do not have the location yet." ?
-
@SGaist I've got confused by my own code.. I'm checking if QSqlDatabase contains "messengerDB", and I thought that it's checking the default location for the database file.
Sounds silly to me right now, but I wrote it a while now, and right now I was in a hurry, and didn't think it through. -
What default location did you had in mind ?