Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Service does not enter the event loop issue
Qt 6.12 LTS is out! See what's new in the release blog

Qt Service does not enter the event loop issue

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 2.2k 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.
  • Cobra91151C Cobra91151

    So, I have found out that even with QCoreApplication *insuranceApp = application(); it does not entering the event loop correctly. What's why it does not execute my code. The question is how to enter the service event loop? Thanks.

    Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #8

    I tried to run this service with the built-in logMessage method:

    void InsuranceService::start()
    {
        logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
        QFile *logFile = new QFile(ServiceLogger::getLogPath());
        logFile->open(QIODevice::WriteOnly | QIODevice::Append);
        ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
        logFile->close();
        logFile->deleteLater();
    }
    

    In the Event Viewer it reports: The start method executing....
    But for some reason everything below logMessage() does not execute. The log file does not exists which should be created. It could be the issue that logMessage() runs on one instance and QFile creates/runs on another instance. I need somehow to connect to the right instance.

    Cobra91151C JonBJ 3 Replies Last reply
    0
    • Cobra91151C Cobra91151

      I tried to run this service with the built-in logMessage method:

      void InsuranceService::start()
      {
          logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
          QFile *logFile = new QFile(ServiceLogger::getLogPath());
          logFile->open(QIODevice::WriteOnly | QIODevice::Append);
          ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
          logFile->close();
          logFile->deleteLater();
      }
      

      In the Event Viewer it reports: The start method executing....
      But for some reason everything below logMessage() does not execute. The log file does not exists which should be created. It could be the issue that logMessage() runs on one instance and QFile creates/runs on another instance. I need somehow to connect to the right instance.

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by
      #9

      Additionally, I created a test method in QtServiceBase class.

      void QtServiceBase::myCustomMessage(QString path)
      {
          QFile *file = new QFile(path);
          file->open(QIODevice::WriteOnly);
          file->write("This is just a test example....");
          file->close();
          file->deleteLater();
      }
      

      And in InsuranceService class I call my myCustomMessage() test method.

      void InsuranceService::start()
      {
          logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
          myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt");
      }
      

      Now it creates the .txt file in the service! So, I was right. The issue is with the wrong instance.
      Have anyone tried the QtService to share some details about this issue? Thanks.

      1 Reply Last reply
      0
      • Cobra91151C Cobra91151

        I tried to run this service with the built-in logMessage method:

        void InsuranceService::start()
        {
            logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
            QFile *logFile = new QFile(ServiceLogger::getLogPath());
            logFile->open(QIODevice::WriteOnly | QIODevice::Append);
            ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
            logFile->close();
            logFile->deleteLater();
        }
        

        In the Event Viewer it reports: The start method executing....
        But for some reason everything below logMessage() does not execute. The log file does not exists which should be created. It could be the issue that logMessage() runs on one instance and QFile creates/runs on another instance. I need somehow to connect to the right instance.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #10

        @Cobra91151 said in Qt Service does not enter the event loop issue:

        But for some reason everything below logMessage() does not execute

        logFile->open(QIODevice::WriteOnly | QIODevice::Append);
        

        How do you know this does not execute? The very first thing I would do is check the return result and report details of any failure.

        Could you have a file permission issue? You have to be careful about the paths you pass to open and the permissions you are currently executing under.

        Cobra91151C 1 Reply Last reply
        1
        • JonBJ JonB

          @Cobra91151 said in Qt Service does not enter the event loop issue:

          But for some reason everything below logMessage() does not execute

          logFile->open(QIODevice::WriteOnly | QIODevice::Append);
          

          How do you know this does not execute? The very first thing I would do is check the return result and report details of any failure.

          Could you have a file permission issue? You have to be careful about the paths you pass to open and the permissions you are currently executing under.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #11

          @JonB

          I just run InsuranceService.exe -exec in cmd and it works beause I see the file is created and data is written there as well. When I run NET START InsuranceService command, the service is started and running but no file is created.

          Also, I add my test method to QtServiceBase class and call it, then the file automatically creates when I start my service. Additionally, the program has the admin privileges.

          I think the issue could be with the static Qt build, which could take some internal files and override them. I had some similar issues with QFtp class. I will try to use Qt shared build and provide all dlls components.

          JonBJ 1 Reply Last reply
          0
          • Cobra91151C Cobra91151

            @JonB

            I just run InsuranceService.exe -exec in cmd and it works beause I see the file is created and data is written there as well. When I run NET START InsuranceService command, the service is started and running but no file is created.

            Also, I add my test method to QtServiceBase class and call it, then the file automatically creates when I start my service. Additionally, the program has the admin privileges.

            I think the issue could be with the static Qt build, which could take some internal files and override them. I had some similar issues with QFtp class. I will try to use Qt shared build and provide all dlls components.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #12

            @Cobra91151 I suggested what I would do.

            Cobra91151C 1 Reply Last reply
            0
            • JonBJ JonB

              @Cobra91151 I suggested what I would do.

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #13

              @JonB

              I already tried to log every step, anything after myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt"); will NOT execute. At this moment I do not know what is the reason for such behaviour. The app runs just fine but when it is the service it does nothing.

              Cobra91151C 1 Reply Last reply
              0
              • Cobra91151C Cobra91151

                @JonB

                I already tried to log every step, anything after myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt"); will NOT execute. At this moment I do not know what is the reason for such behaviour. The app runs just fine but when it is the service it does nothing.

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #14

                @JonB

                It only executes the QtServiceBase methods. I can show you the example, you will see it one moment.

                When I hardcoded the file path to: "C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\text.txt" it successfully created the file. It seems the issue is with: QStandardPaths::writableLocation(QStandardPaths::DataLocation) but I need to debug it again. Thanks.

                1 Reply Last reply
                0
                • Cobra91151C Cobra91151

                  I tried to run this service with the built-in logMessage method:

                  void InsuranceService::start()
                  {
                      logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
                      QFile *logFile = new QFile(ServiceLogger::getLogPath());
                      logFile->open(QIODevice::WriteOnly | QIODevice::Append);
                      ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
                      logFile->close();
                      logFile->deleteLater();
                  }
                  

                  In the Event Viewer it reports: The start method executing....
                  But for some reason everything below logMessage() does not execute. The log file does not exists which should be created. It could be the issue that logMessage() runs on one instance and QFile creates/runs on another instance. I need somehow to connect to the right instance.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #15

                  @Cobra91151 said in Qt Service does not enter the event loop issue:

                  In the Event Viewer it reports: The start method executing....
                  But for some reason everything below logMessage() does not execute.

                  logFile->open(QIODevice::WriteOnly | QIODevice::Append);

                  Why don't you test this result? At the moment you would not know whether anything is executing after logMessage(). That is all I am saying.

                  Cobra91151C 2 Replies Last reply
                  1
                  • JonBJ JonB

                    @Cobra91151 said in Qt Service does not enter the event loop issue:

                    In the Event Viewer it reports: The start method executing....
                    But for some reason everything below logMessage() does not execute.

                    logFile->open(QIODevice::WriteOnly | QIODevice::Append);

                    Why don't you test this result? At the moment you would not know whether anything is executing after logMessage(). That is all I am saying.

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #16

                    @JonB

                    I will debug it again because it seems some code is skipped when running it as a service. Thank you.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Cobra91151 said in Qt Service does not enter the event loop issue:

                      In the Event Viewer it reports: The start method executing....
                      But for some reason everything below logMessage() does not execute.

                      logFile->open(QIODevice::WriteOnly | QIODevice::Append);

                      Why don't you test this result? At the moment you would not know whether anything is executing after logMessage(). That is all I am saying.

                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #17

                      @JonB @ChrisW67

                      Ok. I have properly debug the app and found out that QProcess, QStandardPaths::writableLocation and qgetenv are not working when app is a service. It seems that service functionality is limited. So, I decided to set my app to run at OS startup instead of a service. Now, it works well. The issue is resolved. Thanks.

                      1 Reply Last reply
                      0
                      • Cobra91151C Cobra91151 has marked this topic as solved on
                      • Cobra91151C Cobra91151 has marked this topic as unsolved on
                      • Cobra91151C Cobra91151 has marked this topic as solved on

                      • Login

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