Results 1 to 4 of 4

Thread: Is it possible to use signal, slots and threads in Qtest application?

  1. #1
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Is it possible to use signal, slots and threads in Qtest application?

    Hello everybody,
    I writing my first Unit test application but there are problems when using Signals and slots.
    slots never been called. and also is it possible to test a class inherits QThread.
    Here are my code sample
    Qt Code:
    1. TestSearchEngine::TestSearchEngine()
    2. {
    3. m_pSearchEngine = new SearchEngine();
    4. connect(m_pSearchEngine, SIGNAL(resultsDetected(int)), this, SLOT(onSearchResultDetected(int)));
    5.  
    6. }
    7.  
    8. void TestSearchEngine::testSearch()
    9. {
    10. QStringList searchWords;
    11. searchWords <<tr("sampleword");
    12. QList<int> booksId;
    13. booksId << 11430;
    14. QBENCHMARK{
    15. m_pSearchEngine->search(searchWords, booksId);}
    16. }
    17.  
    18. void TestSearchEngine::onSearchResultDetected(int count)
    19. {
    20. QCOMPARE(count, 3075 );
    21. testSearchResult(1);
    22.  
    23. }
    24.  
    25. void TestSearchEngine::testSearchResult(int index)
    26. {
    27. QBENCHMARK{
    28. m_pSearchEngine->searchResult(index);}
    29. }
    To copy to clipboard, switch view to plain text mode 
    Note that m_pSearchEngine is an object of a class inherits QThread and it starts the thread when calling search function.
    Thank you

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it possible to use signal, slots and threads in Qtest application?

    1. read this: You’re doing it wrong…
    2. when connections between threads are made, then when signal is emitted event is send to another thread. So code which receives signals from another thread have to process event loop. So every time you are expecting signal to be received you have to call QCoreApplication::processEvents or QTest::qWait(200) (I recommend qWait since it may require to wait for this event).

  3. #3
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is it possible to use signal, slots and threads in Qtest application?

    1. read this: You’re doing it wrong…
    I don't think that i use QThread incorrectly and you can see the followinf function
    Qt Code:
    1. void SearchEngine::search(QStringList aSearchWord, QList<int>aBooksIds)
    2. {
    3. m_searchWord = aSearchWord;
    4. m_booksIds = aBooksIds;
    5. if(!isRunning())
    6. {
    7. start();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    i just use the search function to init some variables and also to start the thread.
    2. when connections between threads are made, then when signal is emitted event is send to another thread. So code which receives signals from another thread have to process event loop. So every time you are expecting signal to be received you have to call QCoreApplication:rocessEvents or QTest::qWait(200) (I recommend qWait since it may require to wait for this event).
    i did just as you saied but still the slot never been called and here are the code the used
    Qt Code:
    1. void TestSearchEngine::testSearch()
    2. {
    3. QStringList searchWords;
    4. searchWords << tr("ظ…ط*ظ…ط¯");
    5. QList<int> booksId;
    6. booksId << 11430;
    7. m_pSearchEngine->search(searchWords, booksId);
    8. QCoreApplication::processEvents();
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is it possible to use signal, slots and threads in Qtest application?

    is there any body knows how to test QThread class?

Similar Threads

  1. Threads and slots
    By cafu in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2010, 06:52
  2. threads, signals and slots
    By cyclic in forum Qt Programming
    Replies: 6
    Last Post: 2nd September 2009, 09:44
  3. Signals and Slots Across Threads
    By themusicalguy in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 11:16
  4. signals/slots across threads
    By high_flyer in forum Qt Programming
    Replies: 9
    Last Post: 12th March 2007, 23:56
  5. signal/slots across threads in Qt4
    By Ambiorix in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2006, 08:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.