Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program running with QProcess can't access database
Forum Updated to NodeBB v4.3 + New Features

Program running with QProcess can't access database

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 499 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Linhares
    wrote on 27 Oct 2022, 15:01 last edited by
    #1

    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:

    1. How can I make the game access the database using QProcess::start()?
    2. 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.

    J 2 Replies Last reply 27 Oct 2022, 15:04
    0
    • L Linhares
      27 Oct 2022, 15:01

      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:

      1. How can I make the game access the database using QProcess::start()?
      2. 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.

      J Offline
      J Offline
      JonB
      wrote on 27 Oct 2022, 15:04 last edited by JonB
      #2

      @Linhares
      Everything can/should be doable straight through QProcess. 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?

      C 1 Reply Last reply 27 Oct 2022, 15:08
      1
      • J JonB
        27 Oct 2022, 15:04

        @Linhares
        Everything can/should be doable straight through QProcess. 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?

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 27 Oct 2022, 15:08 last edited by
        #3

        @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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • L Offline
          L Offline
          Linhares
          wrote on 27 Oct 2022, 21:50 last edited by
          #4

          @JonB said in Program running with QProcess can't access database:

          @Linhares
          Everything can/should be doable straight through QProcess. 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 to filePath 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?

          J S 2 Replies Last reply 28 Oct 2022, 05:31
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 27 Oct 2022, 22:15 last edited by
            #5

            @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
            
            1 Reply Last reply
            0
            • L Linhares
              27 Oct 2022, 21:50

              @JonB said in Program running with QProcess can't access database:

              @Linhares
              Everything can/should be doable straight through QProcess. 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 to filePath 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?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 28 Oct 2022, 05:31 last edited by
              #6

              @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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • L Linhares
                27 Oct 2022, 15:01

                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:

                1. How can I make the game access the database using QProcess::start()?
                2. 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.

                J Offline
                J Offline
                JonB
                wrote on 28 Oct 2022, 07:07 last edited by
                #7

                @Linhares
                As @jsulm has written, if you need to set what the current directory is as per the .bat file's cd ..., use QProcess::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's QCoreApplication::applicationDirPath() + "Folder/Subfolder/" or perhaps it's up one level via QCoreApplication::applicationDirPath() + "../Folder/Subfolder/".

                1 Reply Last reply
                1
                • L Offline
                  L Offline
                  Linhares
                  wrote on 28 Oct 2022, 12:59 last edited by
                  #8

                  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);
                  
                  1 Reply Last reply
                  0
                  • L Linhares
                    27 Oct 2022, 21:50

                    @JonB said in Program running with QProcess can't access database:

                    @Linhares
                    Everything can/should be doable straight through QProcess. 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 to filePath 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?

                    S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 28 Oct 2022, 19:20 last edited by
                    #9

                    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 ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    1/9

                    27 Oct 2022, 15:01

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved