PDA

View Full Version : Very high CPU usage with QTableView and MVC



montylee
12th March 2009, 23:44
I am getting a very strange problem in my application and it's driving me nuts :(

I am using a QTableView with a custom model derived from QAbstractTableModel. I also have a separate QTableView which is used only to display the header and 1 row. This table view uses QStandardItemModel.

Now, when i run my application, the CPU usage jumps up to > 90% immediately and remains that way all the time. I tried finding the reason for this by commenting out stuff.

Now, if i comment the tableView->setModel() line, the CPU usage is fine but as soon as i uncomment it, CPU usage jumps up > 90%.

I commented all code which was using my custom model to check if the high CPU usage was because of my model class but it doesn't seem the case.

As i mentioned above, i have another QTableView and it uses the QStandardItemModel to display a header and single row. This is the code for the header table:



QStandardItemModel *model = new QStandardItemModel(1, 2);

// Table Headers
model->setHeaderData(0, Qt::Horizontal, QString("Column1");
model->setHeaderData(1, Qt::Horizontal, QString("Column2");

// Set the header table model
m_pHeaderTableView->setModel(model);

Now, if i comment all code which uses my custom model and use only the header table with QStandardItemModel, the above code works fine and CPU usage is ok but when i set the column widths using the following:



m_pHeaderTableView->setColumnWidth(0, 50);
m_pHeaderTableView->setColumnWidth(1, 50);

When i use setColumnWidth and run my application, the CPU usage is > 90% all the time. If i comment setColumnWidth, CPU usage is fine.

So, in the above case, i am not using my custom model at all. I am using a simple QTableView with QStandardItemModel.

I tried to do the same thing with the other table view which uses my custom model and commenting out setColumnWidth makes no difference to the CPU usage, it remains high all the time.

So, i am not able to find out why my application is using > 90% CPU all the time. If i comment setModel calls in both QTableViews the CPU usage drops to normal but offcourse i can't live without setModel.

wysota
13th March 2009, 00:50
Could you set break points in your code using a debugger and check where exactly does you application spin all the time? Alternatively run your application under a profiler (such as callgrind or gprof) and see what takes the most time.

montylee
13th March 2009, 01:05
Could you set break points in your code using a debugger and check where exactly does you application spin all the time? Alternatively run your application under a profiler (such as callgrind or gprof) and see what takes the most time.
ok let me see if i can do that. I haven't used a profiling tool yet so let me see.
By the way, even if i hide my application, CPU usage is high, so it's not because of drawing etc...

very strange and weird issue...

aamer4yu
13th March 2009, 05:12
Check the data function. May be you are consuming time to fetch data...

EDIT: Oops... u are using standardItemModel...so data fetching is also not in your hands...
prob must be somewhere else.. check for events and how you are handling them..

montylee
16th March 2009, 18:25
ya i guess the problem is somewhere else as the problem is coming with QStandardItemModel as well. I haven't got time to debug this issue yet, have been busy in other things. Will update this thread when i am able to debug this issue.

montylee
17th March 2009, 23:00
Could you set break points in your code using a debugger and check where exactly does you application spin all the time? Alternatively run your application under a profiler (such as callgrind or gprof) and see what takes the most time.
Aah, i found the cause of the problem. I compiled the program with -pg option and used gprof to check which API was taking all the CPU time.

I found that it was QAbstractItemView::timerEvent(QTimerEvent * event). I wanted the contents of the table rows to scroll so i implemented a timer by subclassing QAbstractItemView::timerEvent(QTimerEvent * event) in my class. But due to this, the CPU usage goes high.

I think i'll use a normal QTimer instead of reimplementing QAbstractItemView::timerEvent(QTimerEvent * event).

Don't know why QAbstractItemView::timerEvent(QTimerEvent * event) is causing this problem though.

wysota
18th March 2009, 09:38
I don't think using QTimer will help, it's heavier than a timer event. I'd say you should control the interval properly and stop the timer once you reach your goal although I have no idea what you are doing.

montylee
24th March 2009, 07:14
actually the timer needs to be active as long as the widget is showing because the contents of the table row needs to be scrolled. I can stop the timer when the widget gets hidden.
But still using a timer shouldn't cause the CPU usage to be so high.