Results 1 to 12 of 12

Thread: GUI Thread getting no time to process

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

    Default GUI Thread getting no time to process

    Hi,

    I have created a worker thread from my main gui, the worker thread just does some simple processing and emits a signal to the gui which then processes the data it has been sent. But the worker thread is taking up most of the time available and giving the gui thread no time to do anything. The worker just sleeps for one millisecond ( msleep(1) ), but it has to be this quick as it is responsible for reading CAN traffic off a vehicle, is there anyway of allowing the gui to have more time without changing the msleep?

    Regards,
    Steve

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

    Default Re: GUI Thread getting no time to process

    Can we see the code of the thread and how you start it?

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GUI Thread getting no time to process

    Oh my God...
    I already told you... Emitting a signal from a thread = receiving an event in the interface.
    The interface receives an event every milisec, so, unless you have a pretty good computer, I doubt the event handler will be able to process them all as they come, meaning processing 1000 events in 1 second.

    EDIT: I'm not even sure that all 1000 events are posted all in 1 sec. There might be some delays.

    regards

  4. The following user says thank you to marcel for this useful post:

    steg90 (8th May 2007)

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

    Default Re: GUI Thread getting no time to process

    Hi,

    Yes of course, here is how I create the thread within the main gui :

    Qt Code:
    1. if( !m_pCanReadThread->isRunning() )
    2. m_pCanReadThread->start(QThread::IdlePriority);
    To copy to clipboard, switch view to plain text mode 

    Where m_pCanReadThread is of CanRead object shown below :

    Qt Code:
    1. class CanRead : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. CanRead();
    6. public:
    7. void run();
    8. signals:
    9. void datareceived( int nCount );
    10. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void CanRead::run()
    2. {
    3. static volatile int nCount = 1;
    4. while( nCount < 10000 )
    5. {
    6. // read can data - removed for clarity
    7. msleep(10);
    8. emit datareceived( nCount - 2 );
    9. nCount++;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    The datareceived signal is emitted to the main gui which just updates a tableview.

    Thanks,
    Steve

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

    Default Re: GUI Thread getting no time to process

    So this is impossible?

    I do this in one of my MFC applications and the gui is not 'hogged'?

    Regards,
    Steve

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GUI Thread getting no time to process

    Not impossible.
    One way is to buffer incoming data for, let's say for 500 milisecs( or less ), and then send them all to the GUI in one step and let it update the table view.

    Maybe the guys have other, better, solutions.

  8. #7
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: GUI Thread getting no time to process

    Sounds way too frequent. If your GUI thread is being overloaded, maybe you should just let it ignore signals and continuously process the data to be displayed at its own pace.
    Software Engineer



  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GUI Thread getting no time to process

    But wouldn't it skip some of the data this way?
    Buffering the data in the worker thread and sending a buffer every 500 ms would make items appear rarely but they will appear all.

    Regards

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

    Default Re: GUI Thread getting no time to process

    I'd suggest connecting the signal to a slot which will simply queue the changes and then perform a periodical update of the view that contains all the changes since the last update. And if you're using the model-view approach, avoid inserting one item at a time or block signals from the model during all insertions.

  11. The following user says thank you to wysota for this useful post:

    steg90 (9th May 2007)

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

    Default Re: GUI Thread getting no time to process

    Hi,

    Some interesting suggestions.

    One I'd like to know is how do you block the signal from the model when I'm inserting data? Do I disconnect the signal? The model does emit a signal to my view to tell me that the table needs updating. What I have in my model now is it gets data, stores this in a structure and passes this structure back to the view using a signal, the view then inserts this data into the table and scrolls it. I still have to have a sleep of 10 milliseconds in order for the gui to keep up.

    The code below shows the method that gets signalled in the view to update the table :

    Qt Code:
    1. void CanView::scrolltable( int nCount, CANDATA* pData )
    2. {
    3. for( int i = 0; i < CANAMOUNT; i++ )
    4. {
    5. m_ptreeModel->setCanData( pData[i].m_strId );
    6. m_ptreeModel->setCanData( pData[i].m_strTime );
    7. m_ptreeModel->setCanData( pData[i].m_strData );
    8. QString strCount;
    9. strCount.sprintf( "%i", nCount );
    10. m_ptreeModel->setCanData( strCount );
    11. nCount++;
    12. }
    13.  
    14. QModelIndex index = m_ptreeModel->index( nCount, 0 );
    15. ui.tableView->scrollTo( index );
    16. }
    To copy to clipboard, switch view to plain text mode 

    This is signalled every 10 milliseconds from the worker thread which just basically now reads CAN data into a structure. The setCanData method is a public method within the model :

    Qt Code:
    1. void DATreeModel::setCanData( const QString& strData )
    2. {
    3. QModelIndex index;
    4. beginInsertRows(index, 1, 1);
    5. m_Data.append(strData);
    6. endInsertRows();
    7. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, this inserts one item of data at a time, maybe the structure should be passed across here? Don't know if this would be quicker or not, the method would change to :

    Qt Code:
    1. void DATreeModel::setCanData( CANDATA* pData, int nAmountData, int nCount )
    2. {
    3. QModelIndex index;
    4. beginInsertRows( index, 1, nAmount );
    5. for( int i = 0; i < nAmount; i++ )
    6. {
    7. m_Data.append( pData[i]->m_strId );
    8. m_Data.append( pData[i]->m_strTime );
    9. m_Data.append( pData[i]->m_strData );
    10. QString strCount;
    11. strCount.sprintf( "%i", nCount );
    12. m_Data.append( strCount );
    13. }
    14. endInsertRows();
    15. }
    To copy to clipboard, switch view to plain text mode 

    m_Data is just a QList<QString> container.

    Kind regards,
    Steve

  13. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GUI Thread getting no time to process

    One I'd like to know is how do you block the signal from the model when I'm inserting data? Do I disconnect the signal?
    Use model->blockSignals( true ).
    When you want to activate them again use model->blockSignals( false ).

  14. The following user says thank you to marcel for this useful post:

    steg90 (9th May 2007)

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

    Default Re: GUI Thread getting no time to process

    Hi,

    Turning off the signals AND inserting more than one item at a time in the model has speeded this up no end, I can now set the thread to just sleep for one millisecond and the GUI still got time to process. Many thanks to all!!

    Regards,
    Steve

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  2. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 17:39
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 01:49
  4. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 12:02
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.