[SOLVED] - QStyledItemDelegate - set text color when row is selected
-
Hi,
I have a custom QStyleItemDelegate, and would like to have the default display behavior on all the cells. (selected = blue background, white text, ...)
I have 3 custom widget for editing, and in the paint method I just display some text instead of the widget when theses cell are not being edited.Here is the current Ui:
https://www.dropbox.com/s/vkxb9n0om15m821/DelegateProb.pngWhat I would like is my custom cell to behave like the other normal ones; white text when selected (already working), black text when a cell on the same row is being edited (not working). I have checked the paint code of QStyledItemDelegate.cpp but it's not helping much.
I found this but it's not the same use-case like my QTableView:
http://stackoverflow.com/questions/18568594/correct-highlighting-with-qt-custom-delegates
Would appreciate some help ;)Here is my paint Delegate method :
@void IntervalDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, option.palette.highlight()); painter->setPen(Qt::white); painter->setBrush(option.palette.highlightedText()); } else { painter->setPen(QPen(option.palette.foreground(), 0)); painter->setBrush(qvariant_cast<QBrush>(index.data(Qt::ForegroundRole))); } if (option.state & QStyle::State_HasFocus) { /// Whats the state when editing a cell on the same row? painter->setPen(Qt::red); } /// ------------------------------------- POWER ----------------------------------------------- if (index.column() == 2) { Interval interval = index.model()->data(index, Qt::DisplayRole).value<Interval>(); QString stepTypePower = Interval::getStepTypeToString(interval.getPowerStepType()); int ftpStart = interval.getFTP_start() *100; int ftpEnd = interval.getFTP_end() *100; int range = interval.getFTP_range(); double leftTarget = interval.getLeftPowerTarget(); QString toShow = stepTypePower; if (interval.getPowerStepType() == Interval::PROGRESSIVE) { toShow += " [" + QString::number(ftpStart) + "-" + QString::number(ftpEnd) + "]"; } else if (interval.getPowerStepType() == Interval::FLAT) { toShow += " [" + QString::number(ftpStart) + "]"; } /// Show balance percentage if (leftTarget != -1) { int targetLeftPercent = leftTarget*100; toShow += tr("\nLeft:") + QString::number(targetLeftPercent) + tr(" Right:") + QString::number(100-targetLeftPercent); } QStyledItemDelegate::paint(painter, option, index); /// to get gray background when row selected.. painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow ); } /// ------------------------------------- Cadence ----------------------------------------------- else if (index.column() == 3) { Interval interval = index.model()->data(index, Qt::DisplayRole).value<Interval>(); QString stepTypeCadence = Interval::getStepTypeToString(interval.getCadenceStepType()); int cadenceStart = interval.getCadence_start(); int cadenceEnd = interval.getCadence_end(); int range = interval.getCadence_range(); QString toShow = stepTypeCadence; if (interval.getCadenceStepType() == Interval::PROGRESSIVE) { toShow += " [" + QString::number(cadenceStart) + "-" + QString::number(cadenceEnd) + "]"; } else if (interval.getCadenceStepType() == Interval::FLAT) { toShow += " [" + QString::number(cadenceStart) + "]"; } QStyledItemDelegate::paint(painter, option, index); /// to get gray background when row selected.. painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow ); } /// ------------------------------------- Heart rate ----------------------------------------------- else if (index.column() == 4) { Interval interval = index.model()->data(index, Qt::DisplayRole).value<Interval>(); QString stepTypeHeartrate = Interval::getStepTypeToString(interval.getHRStepType()); int hrStart = interval.getHR_start() * 100; int hrEnd = interval.getHR_end() * 100; int range = interval.getHR_range(); QString toShow = stepTypeHeartrate; if (interval.getHRStepType() == Interval::PROGRESSIVE) { toShow += " [" + QString::number(hrStart) + "-" + QString::number(hrEnd) + "]"; } else if (interval.getHRStepType() == Interval::FLAT) { toShow += " [" + QString::number(hrStart) + "]"; } QStyledItemDelegate::paint(painter, option, index); /// to get gray background when row selected.. painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow ); } else { QStyledItemDelegate::paint(painter, option, index); }
}@
-
If I comment
@QStyledItemDelegate::paint(painter, option, index); @
before
@painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow );@
on the Power cell for example, I get this result:
https://www.dropbox.com/s/q9nu5jsvhr9rxq7/delgatev22.png -
Hi,
It looks like you are using the wrong tool here since you are only drawing text. Why not provide directly the right value from your model ? Or use a QIdentityProxyModel to do the needed transformation ?
-
You are a genius !
I moved all the code from the delegate to my model data function, and use the same code for "role == Qt::DisplayRole".
It's working 100% perfect and no hacking needed in the paint method haha.
Only thing I can't do here is set the text alignement to centered, it is left aligned by defaultthanks you!!!! :)
-
Qt::TextAlignmentRole ?
-
Thanks SGaist, i'm beginning to understand the use of a model now :)
Really powerful!