Results 1 to 6 of 6

Thread: CPU continues to increase until application freezes from QListWidget::addItem

  1. #1
    Join Date
    Dec 2013
    Location
    Phoenix, Az
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default CPU continues to increase until application freezes from QListWidget::addItem

    QT version 4.8.2
    I have created two QListWidget objects that shall contain the Output from the data processing and the Debug issues found while processing the data. As a message is received by the ui thread it is added to the respective ListWidget (via addItem) the message can be as small as a single line of information (summary view and debug ListWidget) to couple hundred lines within a single message (verbose mode). As the number (count) of items increases so does the CPU utilization to a point that a programmatic threshold is achieved whereby the slowdown issues more debug messages which creates more messages (loop). So to attempt to restrict this from happening I have throttled the count to a low number by removing the zero index item in the list. I can live with it, but it is not ideal.

    My question is such... Why is CPU utilization increasing as more items are being added to List? I would understand memory usage to increase. Currently I run at ~20% with GUI, ~7% if built without QT. If I increase the size (max item count) of the ListWidgets to 500 items, as I would like, the CPU jumps to ~70% as the limit approaches. It does not matter whether summary mode or verbose the CPU increases, verbose just gets there faster.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: CPU continues to increase until application freezes from QListWidget::addItem

    CPU usage is increasing because your program is doing something to cause it. Since we cannot see your program there's not much we can say about it.

  3. #3
    Join Date
    Dec 2013
    Location
    Phoenix, Az
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: CPU continues to increase until application freezes from QListWidget::addItem

    Disabling all messages through either AtacsOutputEvent or AtacsMessageEvent and the CPU issue is not seen.

    Qt Code:
    1. AtacsManager.h
    2.  
    3. class AtacsOutputEvent : public QEvent
    4. {
    5. public:
    6. AtacsOutputEvent();
    7. std::string message;
    8. QEvent::Type type() { return (QEvent::Type)outputEvent; }
    9. };
    10.  
    11. class AtacsMessageEvent : public QEvent
    12. {
    13. public:
    14. AtacsMessageEvent();
    15. int level;
    16. string message;
    17. QEvent::Type type() { return (QEvent::Type)statusEvent; }
    18. };
    19.  
    20. AtacsManager.cpp
    21. void AtacsManager::ProcessFrame(long dataType)
    22. {
    23. try {
    24. switch (dataType) {
    25. case kDebugOutput:
    26. {
    27. AtacsOutputEvent *event = new AtacsOutputEvent();
    28. event->message.clear();
    29. event->message.append((const char*)m_AsciiDataType.data);
    30. QCoreApplication::postEvent(m_TargetWidget, event);
    31. break;
    32. }
    33. case kTBDebugMsg:
    34. {
    35. char msg[32];
    36.  
    37. AtacsMessageEvent *event = new AtacsMessageEvent();
    38. event->message.clear();
    39. snprintf(msg, sizeof(msg) - 1, "%d %s ", m_DebugMsg.getLevel(), m_DebugMsg.getTimeString());
    40. event->message.append((const char*)msg);
    41. event->message.append((const char*)m_DebugMsg.getMessage());
    42. QCoreApplication::postEvent(m_TargetWidget, event);
    43. break;
    44. }
    45. } CATCH_CAUSE
    46. }
    47.  
    48. mainwindow.cpp
    49. void MainWindow::customEvent( QEvent * event )
    50. {
    51. switch (event->type()) {
    52. case statusEvent:
    53. {
    54. AtacsMessageEvent *AtacsMessage = (AtacsMessageEvent *)event;
    55. DisplayToDebug((char*)AtacsMessage->message.c_str());
    56. break;
    57. }
    58. case outputEvent:
    59. {
    60. AtacsOutputEvent *AtacsMessage = (AtacsOutputEvent *)event;
    61. DisplayToOutput((char*)AtacsMessage->message.c_str());
    62. break;
    63. }
    64. default:
    65. break;
    66. }
    67. }
    68.  
    69. void MainWindow::DisplayToDebug(char* message)
    70. {
    71. QString qmessage((const char*)message);
    72. ui->DebugLB->addItem(qmessage);
    73. ui->DebugLB->scrollToBottom();
    74. if (ui->DebugLB->count() > 50) {
    75. delete ui->DebugLB->item(0);
    76. }
    77. }
    78.  
    79. void MainWindow::DisplayToOutput(char* message)
    80. {
    81. QString qmessage((const char*)message);
    82. qmessage.chop(1);
    83. ui->OutputLB->addItem(qmessage);
    84. ui->OutputLB->scrollToBottom();
    85. if (ui->OutputLB->count() > 50) {
    86. delete ui->OutputLB->item(0);
    87. }
    88. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by GaryB_Az; 27th December 2013 at 17:05. Reason: Formatting is incorrect

  4. #4
    Join Date
    Dec 2013
    Location
    Phoenix, Az
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: CPU continues to increase until application freezes from QListWidget::addItem

    I don't know if the reason there have been no replies is because there appears to be a work-around but there is not. If I do not send any data to the QListWidget then CPU utilization is not an issue. Putting code up here for others to view in this situation is not beneficial to any one. The question still remains why does CPU utilization increase as items are being added to a QListWidget. If the same messages are only sent to stdio then CPU is at ~6 - 8%, whereas to the Widget it increases to 99% if I don't restrict the number of messages to a small number.

    You have not answered the question.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: CPU continues to increase until application freezes from QListWidget::addItem

    Quote Originally Posted by GaryB_Az View Post
    You have not answered the question.
    "We" are not required to answer the question.

    Why have you used the event system to send messages that would more naturally be handled with a Qt signal and slot?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: CPU continues to increase until application freezes from QListWidget::addItem

    Quote Originally Posted by GaryB_Az View Post
    I don't know if the reason there have been no replies is because there appears to be a work-around but there is not
    Your previous comment seem to indicate that you are no longer seeing the problem.

    Have you tried using a QListView and adding your output messages to a QStringListModel?

    Cheers,
    _

Similar Threads

  1. Application freezes for 0.5 sec after keyPressEvent
    By Coolcat in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2011, 12:16
  2. Segmentation Faul using addItem (QListWidget)
    By gnusar in forum Qt Programming
    Replies: 3
    Last Post: 8th November 2008, 09:27
  3. application freezes without crashing (and closing)
    By nass in forum Qt Programming
    Replies: 15
    Last Post: 25th September 2007, 11:21
  4. QListWidget::addItem and display immediately
    By vlg789 in forum Qt Programming
    Replies: 6
    Last Post: 24th September 2007, 15:30
  5. My SDI application freezes when it is inactive ...why ?
    By yellowmat in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2006, 16:37

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.