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. Calling QCoreApplication::processEvents from QtTest appears not to work
Forum Updated to NodeBB v4.3 + New Features

Calling QCoreApplication::processEvents from QtTest appears not to work

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 370 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.
  • V Offline
    V Offline
    Vaquita Tim
    wrote on last edited by Vaquita Tim
    #1

    This is a question more out of curiosity than anything - maybe I'll understand things better !

    So, I have a logging interface class, CLogger, to which I want to send a QString to be logged from different threads. To ensure thread safety (I prefer my messages not to be interleaved) I have implemented a signal-slot within the CLogger. There is a public CLogger::DoLog( QString ) signal, which is connected to a private CLogger::Log( QString ) slot. To enter a message into the log, I just emit a_Logger.DoLog( "message" ); from any thread.

    This works nicely. Indeed, my unit test just finished running with all passes.

    My question stems from the fact that my initial CLogger connected the signal as a Qt::QueuedConnection.

    This should make no difference, as you are saying. The only thing is that it will queue the log entry in the message loop for the UI thread. And to be honest, I only did it since I thought it might make the UI run more smoothly (just keeping each event processing time to a minumum seems like generally good practice).

    This also works fine in my Application.

    But in QtTest, less so ! Obviously, if the event is queued, after

    emit m_Logger.DoLog( MessageLevel, Message );
    

    The event loop needs to run. So I

    QTest::qWait( 1000 );
    

    However the private slot CLogger::Log( QString ) is not called (I run the test code in debug with a break point) and my test fails to find a new message in the log file.

    I have also tried replacing qWait with

    QElapsedTimer Timer;
    Timer.start();
    do
    {
    	QCoreApplication::processEvents(QEventLoop::AllEvents);
    }
    while( Timer.elapsed() < a_MSecs );
    

    (which is the kind of thing that I suppose QTest::qWait() is doing - given that the documentation says "While waiting, events will be processed...").

    I have used QCoreApplication::processEvents() in the past, and it has worked as I expected - that is although I am currently in the middle of processing one event, new incoming events are processed.

    Am I wrong about processEvents() ? Is it something about QTest which is different ?

    As I said, there is no big problem - I can live with a direct signal-slot connection from the UI thread.

    Aha - I have just spotted
    QWARN : TestLogging::TestEntries() QEventLoop: Cannot be used without QApplication
    Buried near the top of the other messages...

    So I guess my implementaion of QTest has no event loop - which would explain things ;-)

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vaquita Tim
      wrote on last edited by
      #2

      Yes - using QTEST_GUILESS_MAIN(TestLogging) rather than QTEST_APPLESS_MAIN does help to provide an event loop :-D

      1 Reply Last reply
      0
      • V Vaquita Tim 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 Extensions
      • Unsolved