QAbstractItemDelegate::paint() : custom Delegate, how to alter the value displayed in cell - Solved



  • hello all,
    there is probably an easy way to do this: the thing is that in the DB the values of a 'mytype' column are from 1 to 5. these
    values represent constants in the program, so, here is what I have:
    @
    void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const {
    int value = index.model()->data(index, Qt::DisplayRole).toInt();
    switch(value){
    case MyTypes::Type_A:
    //change displayed value to "type A"
    break;
    //.... and so on
    };
    }
    @

    is there a way to do this?
    thx,
    G



  • I think it would be better to put an [[Doc:QAbstractProxyModel]] subclass inbetween (or maybe a [[Doc:QSortFilterProxyModel]] subclass, you'll have to check which one fits best).



  • For Qt 4.7, use a [[Doc:QSortFilterProxyModel]] as your base, for 4.8, a [[Doc:QIdentityProxyModel]]. You don't want to implement the item mapping yourself, I think.



  • well, I solved it by using the painter:
    @
    void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const {
    int value = index.model()->data(index, Qt::DisplayRole).toInt();
    QString text;
    switch(value){
    case MyTypes::Type_A:
    text = "type A";
    break;
    //.... and so on
    };
    QRect r = option.rect.adjusted(2, 2, -2, -2);
    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text, &r);
    @

    it is a quick and dirty solution but will do for the time table I have for this, a proxy model might be a better solution, but, the delegate also handles the editing part of the cell, so, it's a good 'all in one' solution


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.