Results 1 to 6 of 6

Thread: Example of wiggly red-line in QTableView

  1. #1
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default 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 :-)

    Qt Code:
    1. // anomalies are underlined in red, otherwise straight paintjob
    2. void CellDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. QString value = index.model()->data(index, Qt::DisplayRole).toString();
    6.  
    7. // use background shading to show values we have modified
    8. // and can therefore revert to original
    9. if (rideEditor->table->selectionModel()->isSelected(index) == false &&
    10. rideEditor->isEdited(index.row(), index.column())) {
    11. painter->fillRect(option.rect, QBrush(QColor(230,230,230)));
    12. }
    13.  
    14. // found items in yellow
    15. if (rideEditor->isFound(index.row(), index.column()) == true) {
    16. painter->fillRect(option.rect, QBrush(QColor(255,255,0)));
    17. }
    18.  
    19. if (rideEditor->isAnomaly(index.row(), index.column())) {
    20.  
    21. // wavy line is a pain!
    22. QTextDocument *meh = new QTextDocument(QString(value));
    23. wavy.setUnderlineStyle(QTextCharFormat::WaveUnderline);
    24. wavy.setUnderlineColor(Qt::red);
    25. QTextCursor cur = meh->find(value);
    26. cur.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
    27. cur.selectionStart();
    28. cur.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    29. cur.selectionEnd();
    30. cur.setCharFormat(wavy);
    31.  
    32. // only red background if not selected
    33. if (rideEditor->table->selectionModel()->isSelected(index) == false)
    34. painter->fillRect(option.rect, QBrush(QColor(255,230,230)));
    35.  
    36. painter->save();
    37. painter->translate(option.rect.x(), option.rect.y());
    38. meh->drawContents(painter);
    39. painter->restore();
    40. delete meh;
    41. } else {
    42.  
    43. // normal render
    44. QStyleOptionViewItem myOption = option;
    45. myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
    46. drawDisplay(painter, myOption, myOption.rect, value);
    47. drawFocus(painter, myOption, myOption.rect);
    48. }
    49.  
    50. // warning triangle - for high precision numbers
    51. if (rideEditor->isTooPrecise(index.row(), index.column())) {
    52. QPolygon triangle(3);
    53. triangle.putPoints(0, 3, option.rect.x(), option.rect.y(),
    54. option.rect.x()+4, option.rect.y(),
    55. option.rect.x(), option.rect.y()+4);
    56. painter->setBrush(QBrush(QColor(Qt::darkGreen)));
    57. painter->setPen(QPen(QColor(Qt::darkGreen)));
    58. painter->drawPolygon(triangle);
    59. }
    60. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    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.

  3. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Example of wiggly red-line in QTableView

    Any screen shots would be nice

    baray98

  4. #4
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: Example of wiggly red-line in QTableView

    Screenshot-Mark Liversedge.png

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

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  6. #6
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default 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.

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 19:06
  2. How to read and write line by line?
    By Alex Snet in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2010, 15:49
  3. Image reading and writing line by line
    By Astrologer in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2010, 08:15
  4. Replies: 1
    Last Post: 21st November 2009, 08:29
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.