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. Application does not start properly when called on Windows startup
QtWS25 Last Chance

Application does not start properly when called on Windows startup

Scheduled Pinned Locked Moved Solved General and Desktop
windows 10startupsqlite3qtcpsoket
9 Posts 3 Posters 4.1k Views
  • 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.
  • michalosM Offline
    michalosM Offline
    michalos
    wrote on last edited by michalos
    #1

    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.

    A 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      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
      2
      • michalosM michalos

        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.

        A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

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

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        1
        • michalosM Offline
          michalosM Offline
          michalos
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            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
            4
            • michalosM Offline
              michalosM Offline
              michalos
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                What do you mean by "when I do not have the location yet." ?

                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
                • michalosM Offline
                  michalosM Offline
                  michalos
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What default location did you had in mind ?

                    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

                    • Login

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