PDA

View Full Version : CPU continues to increase until application freezes from QListWidget::addItem



GaryB_Az
20th December 2013, 20:08
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.

ChrisW67
21st December 2013, 05:50
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.

GaryB_Az
27th December 2013, 17:02
Disabling all messages through either AtacsOutputEvent or AtacsMessageEvent and the CPU issue is not seen.



AtacsManager.h

class AtacsOutputEvent : public QEvent
{
public:
AtacsOutputEvent();
std::string message;
QEvent::Type type() { return (QEvent::Type)outputEvent; }
};

class AtacsMessageEvent : public QEvent
{
public:
AtacsMessageEvent();
int level;
string message;
QEvent::Type type() { return (QEvent::Type)statusEvent; }
};

AtacsManager.cpp
void AtacsManager::ProcessFrame(long dataType)
{
try {
switch (dataType) {
case kDebugOutput:
{
AtacsOutputEvent *event = new AtacsOutputEvent();
event->message.clear();
event->message.append((const char*)m_AsciiDataType.data);
QCoreApplication::postEvent(m_TargetWidget, event);
break;
}
case kTBDebugMsg:
{
char msg[32];

AtacsMessageEvent *event = new AtacsMessageEvent();
event->message.clear();
snprintf(msg, sizeof(msg) - 1, "%d %s ", m_DebugMsg.getLevel(), m_DebugMsg.getTimeString());
event->message.append((const char*)msg);
event->message.append((const char*)m_DebugMsg.getMessage());
QCoreApplication::postEvent(m_TargetWidget, event);
break;
}
} CATCH_CAUSE
}

mainwindow.cpp
void MainWindow::customEvent( QEvent * event )
{
switch (event->type()) {
case statusEvent:
{
AtacsMessageEvent *AtacsMessage = (AtacsMessageEvent *)event;
DisplayToDebug((char*)AtacsMessage->message.c_str());
break;
}
case outputEvent:
{
AtacsOutputEvent *AtacsMessage = (AtacsOutputEvent *)event;
DisplayToOutput((char*)AtacsMessage->message.c_str());
break;
}
default:
break;
}
}

void MainWindow::DisplayToDebug(char* message)
{
QString qmessage((const char*)message);
ui->DebugLB->addItem(qmessage);
ui->DebugLB->scrollToBottom();
if (ui->DebugLB->count() > 50) {
delete ui->DebugLB->item(0);
}
}

void MainWindow::DisplayToOutput(char* message)
{
QString qmessage((const char*)message);
qmessage.chop(1);
ui->OutputLB->addItem(qmessage);
ui->OutputLB->scrollToBottom();
if (ui->OutputLB->count() > 50) {
delete ui->OutputLB->item(0);
}
}

GaryB_Az
3rd January 2014, 22:33
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.

ChrisW67
4th January 2014, 03:22
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?

anda_skoa
4th January 2014, 11:39
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,
_