×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    QTableView coloring using delegates and related performance

    Ive implemented a QTableView with 4 columns and 6 rows of simple text.
    Ive implemented a delegate(QStyledItemDelegate) like the one above for each column to show each column text in a different colour and display format.
    The scrolling performance of the TableView is horrid even with such little data.
    Is there something wrong with my approach, or a bug in QT , or is there another approach ?

    Any help appreciated. Thanks for your time.
    Last edited by rkurvakat; 2010-03-23 at 17:04.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: QTableView coloring using delegates and related performance

    If I the deregister the delegates the list scrolls smoothly.

    example of my delegate is
    Code:
    void DateItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    	QPalette pal = option.palette;
    	QStyleOptionViewItem  viewOption(option);
    
    	viewOption.displayAlignment = Qt::AlignCenter;
    
    	if(index.column() == 1)
    	{
    		viewOption.palette.setColor(QPalette::Text, Qt::green);
    	} 
    	else if (index.column() == 2) 
    	{
    		viewOption.palette.setColor(QPalette::Text, Qt::red);
    	} 
    
    	QStyledItemDelegate::paint(painter, viewOption, index);
    }
    
    QString DateItemDelegate::displayText(const QVariant & value, const QLocale & locale ) const
    {
    	Q_UNUSED(locale);
    	return QDate::fromString(value.toString(),"yyyyMMdd").toString("dd-MMM-yyyy");
    }
    Last edited by rkurvakat; 2010-03-23 at 18:03.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: QTableView coloring using delegates and related performance

    While using delegates, I had put some logs and those logs indicated my paint() method in the delegate was called as many times as there were items in my table( i.e for each index ), so if I had 2 rows and 4 colums this was called 8 times most often, sometimes less.
    Maybe this is causing the slowdown while using paint() to color the text in the tableview ?
    Is this a bug , that its not optimized in QT or should I have implemented my delegates in some other fashion ?

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableView coloring using delegates and related performance

    First of all, if your goal is to change color of the text you don't need a delegate:
    Code:
    #include <QtGui>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QStandardItemModel model;
    
        for (int i = 0; i < 10; ++i) {
            QStandardItem *item = new QStandardItem(QString("item%1").arg(i));
            model.appendRow(item);
            model.setData(model.index(i, 0), QVariant(QBrush(Qt::red)), Qt::ForegroundRole);
        }
    
        QListView listView;
        listView.setViewMode(QListView::ListMode);
        listView.setMovement(QListView::Free);
        listView.setSelectionMode(QAbstractItemView::SingleSelection);
        listView.setModel(&model);
        listView.show();
    
        return a.exec();
    }
    If you have performance problems you should start with profiling to find the bottleneck in your application. Oprofile is really good tool for profiling:
    http://wiki.maemo.org/Documentation/...aemo5/oprofile

    And finally
    Code:
    QDate::fromString(value.toString(),"yyyyMMdd").toString("dd-MMM-yyyy");
    This thing is performance unfriendly and it's executed before every paint.

    Instead you can do something like this while filling the model:
    Code:
    model.setData(model.index(i, 0), QVariant(strange_code_goes_here), Qt::DisplayRole);
    Last edited by divanov; 2010-03-24 at 09:05.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: QTableView coloring using delegates and related performance

    Quote Originally Posted by divanov View Post
    First of all, if your goal is to change color of the text you don't need a delegate:

    And finally
    Code:
    QDate::fromString(value.toString(),"yyyyMMdd").toString("dd-MMM-yyyy");
    This thing is performance unfriendly and it's executed before every paint.

    Instead you can do something like this while filling the model:
    Code:
    model.setData(model.index(i, 0), QVariant(strange_code_goes_here), Qt::DisplayRole);
    thanks for the tips but not sure if it will help me.
    Sorry but its my fault I should have been clearer with my query.

    Im doing the app for fremantle and Im setting my QTableView using a QSqlTableModel.

    Should I take it from your comment that I need a proxy table model and implement the data() and setData() methods, that would be faster for conversion of db data to view data than using a delegate's displayText() ?

    Also, the coloring of text doesnt work in Fremantle due to the Maemo5 style, which is why I had initially resorted to paint() in the delegate.
    Is there any other better way to color text in the tableview for the data I extract from my SQL table model? seems like even the paint() of the delegate slows down my scrolling performance in the view .

    Thanks

  6. #6
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableView coloring using delegates and related performance

    Quote Originally Posted by rkurvakat View Post
    thanks for the tips but not sure if it will help me.
    Sorry but its my fault I should have been clearer with my query.

    Im doing the app for fremantle and Im setting my QTableView using a QSqlTableModel.
    This is pretty crucial info.

    Quote Originally Posted by rkurvakat View Post
    Should I take it from your comment that I need a proxy table model and implement the data() and setData() methods, that would be faster for conversion of db data to view data than using a delegate's displayText() ?
    Can you tell in more details about Sql database, data conversions and how delegates are involved in this.

    Quote Originally Posted by rkurvakat View Post
    Also, the coloring of text doesnt work in Fremantle due to the Maemo5 style, which is why I had initially resorted to paint() in the delegate.
    This is screenshot of my N900 running the test application.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: QTableView coloring using delegates and related performance

    Quote Originally Posted by divanov View Post
    Can you tell in more details about Sql database, data conversions and how delegates are involved in this.
    this is an example of a delegate that Im using to color and convert for display a date field.
    Code:
    void DateItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    	QPalette pal = option.palette;
    	QStyleOptionViewItem  viewOption(option);
    
    	viewOption.displayAlignment = Qt::AlignCenter;
    
    	if(index.column() == 1)
    	{
    		viewOption.palette.setColor(QPalette::Text, Qt::green);
    	} 
    	else if (index.column() == 2) 
    	{
    		viewOption.palette.setColor(QPalette::Text, Qt::red);
    	} 
    
    	QStyledItemDelegate::paint(painter, viewOption, index);
    }
    
    QString DateItemDelegate::displayText(const QVariant & value, const QLocale & locale ) const
    {
    	Q_UNUSED(locale);
    	return QDate::fromString(value.toString(),"yyyyMMdd").toString("dd-MMM-yyyy");
    }
    This is how im creating a view , model and registering my delegate
    Code:
    _entityTableModel = new QSqlTableModel;
    _entityTableModel->setTable(TABLE);
    _entityTableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    _entityTableModel->select();
    
    
    _entityTableView = new QTableView;
    _entityTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    _entityTableView->setItemDelegateForColumn(1, new DateItemDelegate(this));
    _entityTableView->setItemDelegateForColumn(2, new DateItemDelegate(this));
    _entityTableView->setModel(_entityTableModel);
    this is how I save the field in the Model(DB)
    Code:
    _entityTableModel->setData(_entityTableModel->index(_currentRow, 2), QVariant::fromValue(_inputEDate->date().toString(DatabaseManager::DB_DATE_FORMAT)));
    Last edited by rkurvakat; 2010-03-24 at 23:09.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: QTableView coloring using delegates and related performance

    Ok , I also tried the model proxy tonight, after the delegate example in my previous post.
    Its the same performance, slow !
    I think the issue is with the index() , if we try to check the index in either delegate :: paint() or proxymodel :: data(), the view scrolling slows down drastically, the delegate::displayText is not so much of a performance degrader as I saw it.

    Here is the example I tried with proxyModel for QSqlTableModel, the delegate example you requested is in my previous post below.

    Any help appreciated, I just cant make this view scroll decently with coloured/converted items !

    Code:
    QVariant TableModelProxy::data ( const QModelIndex & index, int role ) const
    {
        if ( index.isValid() && role == Qt::ForegroundRole )
        {
            if ( index.column() == 1 )
                return QVariant( QColor( Qt::green ) );
            else if ( index.column() == 2 )
                return QVariant( QColor( Qt::red ) );
            else if ( index.column() == 3 )
                return QVariant( QColor( Qt::yellow ) );
            else if ( index.column() == 4 )
                return QVariant( QColor( Qt::cyan ) );
        }
    
        return QSqlTableModel::data( index, role );
    }
    
    bool TableModelProxy::setData ( const QModelIndex & index, const QVariant & value, int role )
    {
        return QSqlTableModel::setData(index, value, role);
    }

  9. #9
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableView coloring using delegates and related performance

    It's hard to say something useful without having source code and possibility to try real application. My proposal is to profile with oprofile and then analyse a report. It will allow to determine if this is Qt or your application problem.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: QTableView coloring using delegates and related performance

    Quote Originally Posted by divanov View Post
    It's hard to say something useful without having source code and possibility to try real application. My proposal is to profile with oprofile and then analyse a report. It will allow to determine if this is Qt or your application problem.
    Hi Daniil,

    Sorry to persist, but this is very easy to reproduce I think.
    All you need is to implement a delegate :: paint() or a proxymodel:: data() and register it with one of the columns of the view, you will see the slow scrolling. I tried it yesterday and I could see every index is being passed to the delegate/model and the moment I stop accessing the index() it is fast again. So that makes me think the issue like in the QModelIndex,
    but as you say
    Ill try to upload a very simple working example or will install oprofile on my N900 and analyse/paste the report if that is the better way to proceed with this.

    Thanks for your time again.

  11. #11
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableView coloring using delegates and related performance

    I have a simple test app, but without SQL database and I do not observe any performance issues. If you attach test application here, it would be really great.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    Re: QTableView coloring using delegates and related performance

    hi

    it seems, the root cause for this problem has been found:

    http://blog.rburchell.com/2010/04/qt...o-warning.html

    David

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Re: QTableView coloring using delegates and related performance

    Quote Originally Posted by Goliath23 View Post
    hi
    it seems, the root cause for this problem has been found:
    http://blog.rburchell.com/2010/04/qt...o-warning.html
    David
    are we sure the above is the rootcase for even the delegate issue mentioned below.
    This seems to only indicate the height of the row, but the issues I mentioned below, the row height was maemo standard.
    I know Im yet to provide a simple example to Daniil as requested , I will do so shortly when I get a bit of time :-( .

    thanks.

  14. #14
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableView coloring using delegates and related performance

    You can profile your application with oprofile and just post output of
    Code:
    oprofile -l
    here. It would be enough to tell if it's the same problem or not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×