Results 1 to 5 of 5

Thread: Thread not having anytime?

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Thread not having anytime?

    Hi,

    I have some code which creates a thread which basically just emits a signal back to the worker thread which puts a string into a QListWidget. Problem is, the items in the list widget are not shown until the worker thread has finished what it was doing?

    This is some of the code :

    Qt Code:
    1. void CInitialiseThread::run()
    2. {
    3. while( !m_bStopped )
    4. {
    5. emit AddInfo();
    6. msleep( 10 );
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thread is created in the following function :

    Qt Code:
    1. void CanView::LoadupDetails()
    2. {
    3. if( theApp->m_pWorkspace )
    4. {
    5.  
    6. m_pCanInitialiseThread = new CInitialiseThread();
    7.  
    8. connect( m_pCanInitialiseThread, SIGNAL(AddInfo()), this,
    9. SLOT(AddInfo()) );
    10.  
    11. if( !m_pCanInitialiseThread->isRunning() )
    12. {
    13. m_pCanInitialiseThread->start();
    14. }
    15.  
    16. QList<CCanChannel*>*pcanFilterObj = &theApp->m_pWorkspace->m_oCanChannels;
    17. if( pcanFilterObj )
    18. {
    19. for( int i = 0; i < pcanFilterObj->count(); i++ )
    20. {
    21. CCanChannel* pCanChannel = (CCanChannel*)pcanFilterObj->at( i );
    22. if( pCanChannel )
    23. {
    24. int nChannel = ui.canBox->currentIndex() + 1;
    25. QString strId;
    26. strId.sprintf( "%i", nChannel );
    27. if( pCanChannel->m_strCanChannel == strId )
    28. {
    29. // found channel we want
    30. int nBaud = ui.baudBox->findText( pCanChannel->m_strBaud );
    31. ui.baudBox->setCurrentIndex( nBaud );
    32. if( pCanChannel->m_pCanFilter )
    33. {
    34. // set extended flag and can id flag
    35. //ui.Extended->setChecked( pCanChannel->m_pCanFilter->m_bExtended );
    36. //ui.canIdCheck->setChecked( pCanChannel->m_pCanFilter->m_bFilterOnId );
    37.  
    38. //ui.canIdList->clear();
    39.  
    40. QString strId = "";
    41. // any filtering on canid's?
    42. for( int j = 0; j < pCanChannel->m_oFilterIdList.count(); j++ )
    43. {
    44. // ui.canIdList->addItem( pCanChannel->m_oFilterIdList.at(j) );
    45. int nHex = pCanChannel->m_oFilterIdList.at(j).toInt( 0, 16 );
    46. strId.sprintf( "%x", nHex );
    47. theApp->AddFilterByCanId( strId, theApp->GetChannelID()+1 );//1 );
    48. }
    49.  
    50.  
    51. CCanFilterDetails* pCanDetails;
    52. for( int jj = 0; jj < pCanChannel->m_pCanFilter->m_oCanFilterDetails.count(); jj++ )
    53. {
    54. pCanDetails = (CCanFilterDetails*)pCanChannel->m_pCanFilter->m_oCanFilterDetails.at(jj);
    55.  
    56. // Configure can - does passthruconnect for each filter
    57. long lFlags = 0;
    58. bool ok;
    59. int iMask = pCanDetails->m_strMask.toLong( &ok, 16 );
    60. int iPattern = pCanDetails->m_strAccept.toLong( &ok, 16 );
    61.  
    62. if( pCanChannel->m_pCanFilter->m_bExtended )
    63. lFlags = 0x100;
    64.  
    65. QString strFilterType = pCanDetails->m_strType;
    66. if( pCanChannel->m_pCanFilter->m_bExtended )
    67. lFlags = 0x100;
    68.  
    69. QString strCanChannel = pCanChannel->m_strCanChannel;// ui.canChannels->currentText();
    70. int nBaud = pCanChannel->m_strBaud.toInt();//ui.baudRate->currentText().toInt();
    71. int iReply = theApp->ConfigureCan( nBaud, nBaud, strCanChannel ); // configre can first
    72.  
    73. unsigned long iFilterId = SetFilter( pCanDetails->m_strMask, pCanDetails->m_strAccept, lFlags, 1, TRUE );
    74.  
    75.  
    76. }
    77. }
    78.  
    79. break;
    80. }
    81. }
    82.  
    83. }
    84. }
    85. }
    86. if( m_pCanInitialiseThread->isRunning() )
    87. {
    88. m_pCanInitialiseThread->stop();
    89. m_pCanInitialiseThread->wait();
    90. }
    91. }
    To copy to clipboard, switch view to plain text mode 

    The Addinfo basically just adds a QString to a QListWidget. I don't know why the QListWidget isn't being updated every 10ms?

    Guess I'm doing something wrong or there is no CPU time?

    Regards,
    Steve

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread not having anytime?

    You are not letting the event loop deliver the signal. You just have a long block of code where you start a thread then do something and then stop the thread. And you should start the thread, do something, return to the event loop (at this point signals will start to be delivered to your main thread) and when the worker thread signals that it is done doing its job, call stop and wait. So at least you should put the stop() and wait() calls into another slot connected to one of the thread's signals.

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread not having anytime?

    Hi,

    The signal seems to be getting delivered, it seems it is the QListWidget not updating - I have since put in QCoreApplication:rocessEvents() and this seems to help. I thought I could just start the thread and it would run and the signal would be delivered. I see what you are saying, the thread emits the signal, but the event loop cannot process it due to still running my other block of code.

    Regards,
    Steve

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread not having anytime?

    By "delivered" I meant "processed by the receiver". Maybe I should have said it more clearly, sorry

  5. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread not having anytime?

    Lol. Thanks for your help, always appreciated

    Steve

Similar Threads

  1. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 15:31
  2. Terminating a thread.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2007, 11:14
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

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
  •  
Qt is a trademark of The Qt Company.