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. How to properly start Qt with Qt Eventloop for GTest
Forum Updated to NodeBB v4.3 + New Features

How to properly start Qt with Qt Eventloop for GTest

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.3k 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.
  • J Offline
    J Offline
    JuliusCaesar
    wrote on last edited by
    #1

    Hey,
    I'd like to create some GTests on QT Objects. However, if need the Signals/Slots to work.
    Therefore I run QApplication in main() and from main() I spawn a QThread() running Gtest.
    I'm running Qt 5.12.8

    /**
     * @brief Executes GTest. After Executing GTest it will stop the QApllication
    */
    static void ExecuteGTest();
    
    int main(int argc, char* argv[])
    {
    
    
      ::testing::InitGoogleTest(&argc, argv);
      QApplication app(argc, argv);
    
      std::unique_ptr<QThread> pGTest(QThread::create(ExecuteGTest));
      pGTest->start();
    
      const int iGTestRes = app.exec();
      bool bIsRunning = pGTest->isRunning();
      if (bIsRunning)
      {
        //GTest thread is still running =>should not been happening....requesting exit
        //Request exit with error code
        pGTest->exit(-1);
        pGTest->wait();
      }
      return iGTestRes;
    }
    
    static void ExecuteGTest()
    {
      {
        //perform checks to validate that Qt Eventloop is actually running
        const QThread* pCurrent = QThread::currentThread();
        ASSERT_TRUE(nullptr != pCurrent) << "This is no QThread";
        const auto* pEventLoop = pCurrent->eventDispatcher();
        ASSERT_TRUE(nullptr != pEventLoop) << "Eventloop is not running";
      }
      const int iRes = RUN_ALL_TESTS();
      //terminate the Qt Eventloop and return GTest Result
      QApplication::exit(iRes);
    }
    

    Now my Testcase is pretty simple

    static bool mg_bTimerExpired = false;
    
    static void OnTimerTick();
    
    static void OnTimerTick()
    {
      mg_bTimerExpired = false;
    }
    
    
    
    TEST(RunCameraUpdate, FirstTest)
    {
      QTimer::singleShot(2000, nullptr, &OnTimerTick);
      
      std::this_thread::sleep_for(std::chrono::seconds(5));
    
      EXPECT_TRUE(mg_bTimerExpired);
    }
    

    Unfortunately OnTimerTick is never executed. Do you have an idea why?

    thx for your help :)

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      @JuliusCaesar said in How to properly start Qt with Qt Eventloop for GTest:

      std::this_thread::sleep_for(std::chrono::seconds(5));

      You block the event loop here so no signals can be send.

      /edit: you're likely looking for QSignalSpy

      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
      • J Offline
        J Offline
        JuliusCaesar
        wrote on last edited by
        #3

        bit this code is run from a new spawned QThread.
        Doesn't the Eventloop run in the main Thread()?

        I tried

        QThread* pCurrent = QThread::currentThread();
          pCurrent->sleep(5);
        

        as well. This does not work either
        Thx for the Tip with SignalSpy. I'll have a look at that :)

        Christian EhrlicherC 1 Reply Last reply
        0
        • J JuliusCaesar

          bit this code is run from a new spawned QThread.
          Doesn't the Eventloop run in the main Thread()?

          I tried

          QThread* pCurrent = QThread::currentThread();
            pCurrent->sleep(5);
          

          as well. This does not work either
          Thx for the Tip with SignalSpy. I'll have a look at that :)

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JuliusCaesar said in How to properly start Qt with Qt Eventloop for GTest:

          Doesn't the Eventloop run in the main Thread()?

          Every QThread has it's own eventloop when you don't overwrite run. See the documentation and objects create in a QThread have to use this eventloop. Otherwise a separate QThread would be useless.

          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
          • J Offline
            J Offline
            JuliusCaesar
            wrote on last edited by
            #5

            Hey,
            thx for Your reply :)

            When I manually call QCoreApplication::processEvents() after the wait time, all pending events are processed.
            So this is a backup solution.

            But I do not want to call processEvents in each testcase.

            Is it possible to have 2 QThreads. One QThread with an working event which holds all QObjects and 1 where all Tests run in.
            Or is there any other solution? Could you help finding my figuring this out?

            How do non QT GUI applications process the event handling?

            thx :)
            greets

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JuliusCaesar said in How to properly start Qt with Qt Eventloop for GTest:

              How do non QT GUI applications process the event handling?

              Exactly like the gui ones - the don't block the event loop and you should not do either. There is no reason for it.

              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
              • J Offline
                J Offline
                JuliusCaesar
                wrote on last edited by
                #7

                alright thx for your answer. I'll have to think about an alternate solution :)

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Ccyros
                  wrote on last edited by
                  #8

                  Check my example here:
                  https://forum.qt.io/post/804638

                  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