Results 1 to 8 of 8

Thread: Very high CPU usage with QTableView and MVC

  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Very high CPU usage with QTableView and MVC

    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:

    Qt Code:
    1.  
    2. // Table Headers
    3. model->setHeaderData(0, Qt::Horizontal, QString("Column1");
    4. model->setHeaderData(1, Qt::Horizontal, QString("Column2");
    5.  
    6. // Set the header table model
    7. m_pHeaderTableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. m_pHeaderTableView->setColumnWidth(0, 50);
    2. m_pHeaderTableView->setColumnWidth(1, 50);
    To copy to clipboard, switch view to plain text mode 

    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.

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

    Default Re: Very high CPU usage with QTableView and MVC

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    montylee (17th March 2009)

  4. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Very high CPU usage with QTableView and MVC

    Quote Originally Posted by wysota View Post
    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...

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Very high CPU usage with QTableView and MVC

    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..

  6. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Very high CPU usage with QTableView and MVC

    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.

  7. #6
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Very high CPU usage with QTableView and MVC

    Quote Originally Posted by wysota View Post
    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.

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

    Default Re: Very high CPU usage with QTableView and MVC

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Very high CPU usage with QTableView and MVC

    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.

Tags for this Thread

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.