PDA

View Full Version : Example of wiggly red-line in QTableView



liversedge
6th June 2010, 08:49
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 :-)


// anomalies are underlined in red, otherwise straight paintjob
void CellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
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) {
painter->fillRect(option.rect, QBrush(QColor(255,255,0)));
}

if (rideEditor->isAnomaly(index.row(), index.column())) {

// wavy line is a pain!
QTextDocument *meh = new QTextDocument(QString(value));
QTextCharFormat wavy;
wavy.setUnderlineStyle(QTextCharFormat::WaveUnderl ine);
wavy.setUnderlineColor(Qt::red);
QTextCursor cur = meh->find(value);
cur.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
cur.selectionStart();
cur.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
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
QStyleOptionViewItem myOption = option;
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())) {
QPolygon triangle(3);
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->setBrush(QBrush(QColor(Qt::darkGreen)));
painter->setPen(QPen(QColor(Qt::darkGreen)));
painter->drawPolygon(triangle);
}
}

stefanadelbert
15th July 2010, 01:55
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:

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.

baray98
15th July 2010, 06:43
Any screen shots would be nice

baray98

liversedge
27th July 2010, 22:46
5005

Apologies for not responding sooner, just caught up :-)

aamer4yu
28th July 2010, 05:49
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:

liversedge
28th July 2010, 07:15
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.