Example of wiggly red-line in QTableView
Hi,
I'm writing an app that needs to show a wavy-line under cell values that have been identified as anomalies. I got there in the end, and whilst it might not be the most elegant code, it works. I suffered lots of issues getting it to work and thought it might be useful to share here.
Basically, the item delegate's paint function colors the cell or puts a wavy line under the value or puts a little green triangle in the corner of the cell.
Hope someone finds it useful or can tell me how to improve it :-)
Code:
// anomalies are underlined in red, otherwise straight paintjob
{
QString value
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
// use background shading to show values we have modified
// and can therefore revert to original
if (rideEditor->table->selectionModel()->isSelected(index) == false &&
rideEditor->isEdited(index.row(), index.column())) {
painter
->fillRect
(option.
rect,
QBrush(QColor(230,
230,
230)));
}
// found items in yellow
if (rideEditor->isFound(index.row(), index.column()) == true) {
}
if (rideEditor->isAnomaly(index.row(), index.column())) {
// wavy line is a pain!
wavy.setUnderlineColor(Qt::red);
cur.selectionStart();
cur.selectionEnd();
cur.setCharFormat(wavy);
// only red background if not selected
if (rideEditor->table->selectionModel()->isSelected(index) == false)
painter
->fillRect
(option.
rect,
QBrush(QColor(255,
230,
230)));
painter->save();
painter->translate(option.rect.x(), option.rect.y());
meh->drawContents(painter);
painter->restore();
delete meh;
} else {
// normal render
myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, value);
drawFocus(painter, myOption, myOption.rect);
}
// warning triangle - for high precision numbers
if (rideEditor->isTooPrecise(index.row(), index.column())) {
triangle.putPoints(0, 3, option.rect.x(), option.rect.y(),
option.rect.x()+4, option.rect.y(),
option.rect.x(), option.rect.y()+4);
painter->drawPolygon(triangle);
}
}
Re: Example of wiggly red-line in QTableView
This is useful, thanks for posting.
On the bit of reading that I've done, it's been recommended to restore the painter to its original state once you're done painting. You could call painter->save() before painting and then painter->restore() once you're done. You do do this in one particular instance, but it might be a good idea to do it for the other instances too.
Not sure what effect it actually has, but it does seem to be best practice.
QStyledItemDelegate Class Reference:
Quote:
After painting, you should ensure that the painter is returned to its the state it was supplied in when this function was called. For example, it may be useful to call QPainter::save() before painting and QPainter::restore() afterwards.
Re: Example of wiggly red-line in QTableView
Any screen shots would be nice
baray98
1 Attachment(s)
Re: Example of wiggly red-line in QTableView
Attachment 5005
Apologies for not responding sooner, just caught up :-)
Re: Example of wiggly red-line in QTableView
One question.. why not change the color of whole text ?
Usually wiggly red line is used to meant some grammatical error.
So there are chances user gets confused :rolleyes:
Re: Example of wiggly red-line in QTableView
In my example it is highlighting anomalies in a numerical data series. The user would have to be especially stupid to think it was a grammatical error. In fact, so stupid they probably wouldn't understand the word anomaly, or perhaps even example.