Results 1 to 7 of 7

Thread: QTableView perfomance 2

  1. #1
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView perfomance 2

    Hi,

    I have a QTableView with over 10000 entries.

    I have derived from QAbstractTableModel a custom model with this data() member function

    Qt Code:
    1. QVariant log_q_item_model::data(const QModelIndex& index, int role) const
    2. {
    3. if (index.isValid() && index.row()>=0 && static_cast<size_t>(index.row())<log_->size() && role==Qt::DisplayRole) {
    4. const logentry& e = (*log_)[index.row()];
    5.  
    6. return (index.column()==0)?
    7. QString(boost::gregorian::to_simple_string(e.get_date()).c_str()):
    8. QString::number(e.get_total(),'f',2);
    9. }
    10.  
    11. return QVariant();
    12. }
    To copy to clipboard, switch view to plain text mode 

    The model has a private
    Qt Code:
    1. shared_ptr< vector<logentry> >
    To copy to clipboard, switch view to plain text mode 
    which points to a vector of
    Qt Code:
    1. logentry
    To copy to clipboard, switch view to plain text mode 
    .
    The
    Qt Code:
    1. get_date()
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. get_total()
    To copy to clipboard, switch view to plain text mode 
    are inline functions that return members of logentry.

    I have commented out the implementation of the
    Qt Code:
    1. data()
    To copy to clipboard, switch view to plain text mode 
    function and the display of the TableView widget (all blanks) takes now half a second, instead of 5 seconds.

    My bottleneck is definitely here.

    I have replaced the return statement by
    Qt Code:
    1. return QString::number(e.get_total(),'f',2);
    To copy to clipboard, switch view to plain text mode 

    and it takes half the time roughly.

    It seems the QString functions are in my case quite slow.

    Ideas for speeding this up are appreciated?

    Regards,

  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: QTableView perfomance 2

    Normally data() would only be called when a cell was needed for display by the view. However, if you set the view to resize to contents then a fetch of every cell to allow calculation of an "optimal" size hint results. Given the reasonably fixed presentation of your values you could use the view delegate to pre-calculate a size hint and return a fixed value for the date column, and another for the number columns.

    Other thoughts:
    For the date, there's a conversion to UTF-8 in the QString::QString ( const char * str ) constructor that might avoided with QString::fromAscii() or one of the other equivalents.

    There's also a conversion from QString to QVariant involved.

  3. #3
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView perfomance 2

    Hi,

    Thanks for your advice,

    Other thoughts:
    For the date, there's a conversion to UTF-8 in the QString::QString ( const char * str ) constructor that might avoided with QString::fromAscii() or one of the other equivalents.
    Changing this didn't result in any change.

    Normally data() would only be called when a cell was needed for display by the view. However, if you set the view to resize to contents then a fetch of every cell to allow calculation of an "optimal" size hint results. Given the reasonably fixed presentation of your values you could use the view delegate to pre-calculate a size hint and return a fixed value for the date column, and another for the number columns.
    I've overriden the
    Qt Code:
    1. int QTableView::sizeHintForColumn ( int column ) const
    To copy to clipboard, switch view to plain text mode 
    method of QTableView to be an inline function that returns 100.
    No change either. Is this what you meant?

    Regards,

  4. #4
    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: QTableView perfomance 2

    .. or QStyledItemDelegate::sizeHint()

    Exactly when is the long delay being seen?
    First displaying the QTableView, while scrolling, some other time?
    Are you calling QTableView::resizeColumnToContents ( int column ) or QTableView::resizeColumnsToContents()?
    Do you have QHeaderView::ResizeToContents set on the horizontal header?

    Have you tried giving columns a fixed width with QHeaderView::resizeSection() on the table's horizontal header?

    Can you post the code that sets up your QTableView?

  5. #5
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView perfomance 2

    I'll post the code now, and will answer more in details on monday.

    Qt Code:
    1. class log_q_table_view : public QTableView {
    2. public:
    3. log_q_table_view(const log_ptr& log, QWidget* parent =0);
    4. };
    5.  
    6.  
    7. class log_q_item_model : public QAbstractTableModel {
    8.  
    9. Q_OBJECT
    10.  
    11. public:
    12. log_q_item_model(const log_ptr& log)
    13. : log_(log)
    14. {}
    15.  
    16. int rowCount(const QModelIndex&) const
    17. {
    18. return log_->size();
    19. }
    20. int columnCount(const QModelIndex&) const
    21. {
    22. return 2;
    23. }
    24. QVariant data(const QModelIndex& index, int role) const;
    25.  
    26. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    27.  
    28. private:
    29. const log_ptr log_;
    30. };
    31.  
    32. log_q_table_view::log_q_table_view(const log_ptr& log, QWidget* parent)
    33. : QTableView(parent)
    34. {
    35. setEditTriggers(QAbstractItemView::NoEditTriggers);
    36. setModel(new log_q_item_model(log));
    37. verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    38. horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    39. }
    To copy to clipboard, switch view to plain text mode 

    thanks,

  6. #6
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView perfomance 2

    .. or QStyledItemDelegate::sizeHint()
    I will try.

    Exactly when is the long delay being seen?
    First displaying the QTableView, while scrolling, some other time?
    Right before displaying and when resizing the parent widget. Scrolling is not slow.

    Are you calling QTableView::resizeColumnToContents ( int column ) or QTableView::resizeColumnsToContents()?
    No

    Do you have QHeaderView::ResizeToContents set on the horizontal header?
    Indeed. In the ctor. I will turn this off and see the effect.

    Have you tried giving columns a fixed width with QHeaderView::resizeSection() on the table's horizontal header?
    I will try this as well.

    Thanks,

  7. #7
    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: QTableView perfomance 2

    The use of QHeaderView::ResizeToContents, in my experience, makes the start up time of a table view on a large model unacceptable. To achieve this, every value in the model is fetched, its width calculated, and the largest value in the row/column used to set the section width. For a 10,000 row by 10 column table this means that your model's data() method is called 100,000 times to fetch the display value, and probably a few more times for each cell to get formatting information (Font, decoration, size hint roles). It's possible that using it on both rows and columns repeats the table scan twice. If you can possibly live with a fixed width then you'll find this faster.

Similar Threads

  1. Get the value from QTableview
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 09:55
  2. Replies: 2
    Last Post: 26th November 2009, 04:45
  3. QTableView
    By waynew in forum Newbie
    Replies: 1
    Last Post: 26th November 2009, 04:10
  4. QTableView help
    By weaver4 in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2009, 22:57
  5. QTableView - Add Row
    By maxpower in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2006, 18:18

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.