Program running with QProcess can't access database
-
wrote on 27 Oct 2022, 15:01 last edited by
I'm running an external program (a game) using this command:
QDir directory("Folder/Subfolder"); QString path = directory.filePath("program.exe"); pointer = new QProcess(this); //pointer declared in the .h file pointer->start(path);
The game runs correctly, but it can't access the database (scores) that is in a file.
It can access the database if I run the game directly from the folder, outside from the main program.I tried several strategies for making it access the database and the only one that worked was calling the game from a batch file. However, doing this opens a console window that I don't want to be visible for the user.
Questions:
- How can I make the game access the database using QProcess::start()?
- Is it possible to hide the console window if I call the game from the batch file?
I'm using Qt 5.15, Windows 10.
-
I'm running an external program (a game) using this command:
QDir directory("Folder/Subfolder"); QString path = directory.filePath("program.exe"); pointer = new QProcess(this); //pointer declared in the .h file pointer->start(path);
The game runs correctly, but it can't access the database (scores) that is in a file.
It can access the database if I run the game directly from the folder, outside from the main program.I tried several strategies for making it access the database and the only one that worked was calling the game from a batch file. However, doing this opens a console window that I don't want to be visible for the user.
Questions:
- How can I make the game access the database using QProcess::start()?
- Is it possible to hide the console window if I call the game from the batch file?
I'm using Qt 5.15, Windows 10.
wrote on 27 Oct 2022, 15:04 last edited by JonB@Linhares
Everything can/should be doable straight throughQProcess
. What does "but it can't access the database (scores) that is in a file." mean? If a.bat
file made it work, what did that have in it?You show a relative path in
directory
. This is generally a bad idea, as you rely on but do not know what the current directory is.Purely hazarding a guess, does your database program rely on a file (database? "scores"?) being in the current directory and you don't know what current directory it is inheriting from your running Qt application?
-
@Linhares
Everything can/should be doable straight throughQProcess
. What does "but it can't access the database (scores) that is in a file." mean? If a.bat
file made it work, what did that have in it?You show a relative path in
directory
. This is generally a bad idea, as you rely on but do not know what the current directory is.Purely hazarding a guess, does your database program rely on a file (database? "scores"?) being in the current directory and you don't know what current directory it is inheriting from your running Qt application?
@JonB said in Program running with QProcess can't access database:
You show a relative path in directory. This is generally a bad idea, as you rely on but do not know what the current directory is.
And that's imo also the problem with the external program - it's not started from the correct directory so it can't find either it's config file or (if it's a sqlite database) the database since it's getting searched into the current directory. But just a wild guess.
-
wrote on 27 Oct 2022, 21:50 last edited by
@JonB said in Program running with QProcess can't access database:
@Linhares
Everything can/should be doable straight throughQProcess
. What does "but it can't access the database (scores) that is in a file." mean? If a.bat
file made it work, what did that have in it?The
bat
file has something like this:cd Folder\Subfolder\ program.exe
You show a relative path in
directory
. This is generally a bad idea, as you rely on but do not know what the current directory is.I was using
QDir::absoluteFilePath
before, but I switched tofilePath
because the function wouldn't work if the user put the program in a folder that had spaces in the path. (suggestions on this would be helpful too)Purely hazarding a guess, does your database program rely on a file (database? "scores"?) being in the current directory and you don't know what current directory it is inheriting from your running Qt application?
The database file is in the same folder as the program that was called (program.exe, located in Folder\Subfolder)
@Christian-Ehrlicher said in Program running with QProcess can't access database:
@JonB said in Program running with QProcess can't access database:
You show a relative path in directory. This is generally a bad idea, as you rely on but do not know what the current directory is.
And that's imo also the problem with the external program - it's not started from the correct directory so it can't find either it's config file or (if it's a sqlite database) the database since it's getting searched into the current directory. But just a wild guess.
The program is set to create a new database file if it can't find one. If that you say was the case, the program would probably create the database file in the main program's folder -- which it doesn't.
Could it have something to do with permissions? Like the main program callig the other program without giving it the necessary permissions to read and write files?
-
wrote on 27 Oct 2022, 22:15 last edited by
@Linhares said in Program running with QProcess can't access database:
Is it possible to hide the console window if I call the game from the batch file?
Use start?
@echo off cd working_dir start program.exe
-
@JonB said in Program running with QProcess can't access database:
@Linhares
Everything can/should be doable straight throughQProcess
. What does "but it can't access the database (scores) that is in a file." mean? If a.bat
file made it work, what did that have in it?The
bat
file has something like this:cd Folder\Subfolder\ program.exe
You show a relative path in
directory
. This is generally a bad idea, as you rely on but do not know what the current directory is.I was using
QDir::absoluteFilePath
before, but I switched tofilePath
because the function wouldn't work if the user put the program in a folder that had spaces in the path. (suggestions on this would be helpful too)Purely hazarding a guess, does your database program rely on a file (database? "scores"?) being in the current directory and you don't know what current directory it is inheriting from your running Qt application?
The database file is in the same folder as the program that was called (program.exe, located in Folder\Subfolder)
@Christian-Ehrlicher said in Program running with QProcess can't access database:
@JonB said in Program running with QProcess can't access database:
You show a relative path in directory. This is generally a bad idea, as you rely on but do not know what the current directory is.
And that's imo also the problem with the external program - it's not started from the correct directory so it can't find either it's config file or (if it's a sqlite database) the database since it's getting searched into the current directory. But just a wild guess.
The program is set to create a new database file if it can't find one. If that you say was the case, the program would probably create the database file in the main program's folder -- which it doesn't.
Could it have something to do with permissions? Like the main program callig the other program without giving it the necessary permissions to read and write files?
@Linhares said in Program running with QProcess can't access database:
The database file is in the same folder as the program
Try to set https://doc.qt.io/qt-6/qprocess.html#setWorkingDirectory to the folder where the database is located before starting the other application.
-
I'm running an external program (a game) using this command:
QDir directory("Folder/Subfolder"); QString path = directory.filePath("program.exe"); pointer = new QProcess(this); //pointer declared in the .h file pointer->start(path);
The game runs correctly, but it can't access the database (scores) that is in a file.
It can access the database if I run the game directly from the folder, outside from the main program.I tried several strategies for making it access the database and the only one that worked was calling the game from a batch file. However, doing this opens a console window that I don't want to be visible for the user.
Questions:
- How can I make the game access the database using QProcess::start()?
- Is it possible to hide the console window if I call the game from the batch file?
I'm using Qt 5.15, Windows 10.
wrote on 28 Oct 2022, 07:07 last edited by@Linhares
As @jsulm has written, if you need to set what the current directory is as per the.bat
file'scd ...
, useQProcess::setWorkingDirectory()
to do just that, so that the sub-process is in the same situation as it would be from the commands in the.bat
file.Note that the command you show,
cd Folder\Subfolder\
, uses a relative path. If that is the case, it would be relative to wherever you run that.bat
file from, e.g. whatever the current directory is when you run it from a Command Prompt. You do not know/cannot rely on the current directory when your Qt program is run, it may vary, e.g. it may well be different when run outside Qt Creator versus from Creator. If you do not want to hard-code a full path, the only directory you know at runtime from your Qt program is QCoreApplication::applicationDirPath(). Try to use this to build a path to the directory desired for the sub-process, e.g. maybe it'sQCoreApplication::applicationDirPath() + "Folder/Subfolder/"
or perhaps it's up one level viaQCoreApplication::applicationDirPath() + "../Folder/Subfolder/"
. -
wrote on 28 Oct 2022, 12:59 last edited by
Thanks for the help, everyone.
The solution proposed by @jsulm did the job!
Here's a snippet of the working solution, in case it can help someone in the future:
QDir directory("Folder/Subfolder"); QString path = directory.path(); QString filePath = directory.filePath("program.exe"); myProcess = new QProcess(this); //pointer declared in .h myProcess->setWorkingDirectory(path); myProcess->start(filePath);
-
@JonB said in Program running with QProcess can't access database:
@Linhares
Everything can/should be doable straight throughQProcess
. What does "but it can't access the database (scores) that is in a file." mean? If a.bat
file made it work, what did that have in it?The
bat
file has something like this:cd Folder\Subfolder\ program.exe
You show a relative path in
directory
. This is generally a bad idea, as you rely on but do not know what the current directory is.I was using
QDir::absoluteFilePath
before, but I switched tofilePath
because the function wouldn't work if the user put the program in a folder that had spaces in the path. (suggestions on this would be helpful too)Purely hazarding a guess, does your database program rely on a file (database? "scores"?) being in the current directory and you don't know what current directory it is inheriting from your running Qt application?
The database file is in the same folder as the program that was called (program.exe, located in Folder\Subfolder)
@Christian-Ehrlicher said in Program running with QProcess can't access database:
@JonB said in Program running with QProcess can't access database:
You show a relative path in directory. This is generally a bad idea, as you rely on but do not know what the current directory is.
And that's imo also the problem with the external program - it's not started from the correct directory so it can't find either it's config file or (if it's a sqlite database) the database since it's getting searched into the current directory. But just a wild guess.
The program is set to create a new database file if it can't find one. If that you say was the case, the program would probably create the database file in the main program's folder -- which it doesn't.
Could it have something to do with permissions? Like the main program callig the other program without giving it the necessary permissions to read and write files?
Hi,
@Linhares said in Program running with QProcess can't access database:
The program is set to create a new database file if it can't find one. If that you say was the case, the program would probably create the database file in the main program's folder -- which it doesn't.
Out of curiosity, you write about a database file, is it SQLight ? If so, why are you using an external script to creat it rather that your application directly ?
1/9