[Interest] Custom indicator on QTableWidgetItem

Tony Rietwyk tony at rightsoft.com.au
Mon Nov 20 17:12:17 CET 2017


Hi Sean,

You can use a QStyledItemDelegate, and override the paint method.  Call 
the base method to clear the cell and draw the text, then add your 
indicator based on the role.  Eg:

void CellDelegate::paint(QPainter *painter,  const QStyleOptionViewItem &option,  const QModelIndex &index) const
{
     QStyledItemDelegate::paint( painter, option, index );

     if (index->data(CellCommentIndicatorRole).toBool())

     {

         auto opt = option;
         initStyleOption( &opt, index );

         // opt.rect is the cell rectangle

         // use opt.state to vary the color depending on whether disabled, selected, etc.

         // draw triangle

     }

}

Hope that helps, Tony



On 21/11/2017 2:29 AM, Murphy, Sean wrote:
> Some spreadsheet applications (notably MS Excel, Google Sheets, etc.) have a mechanism for indicating that a cell has a comment attached to it (see attached screenshot, the red triangle in the upper corner of the cell A1). Is there a way to do something similar with a QTableWidgetItem?
>
> The only method I'm seeing at the moment is to create my own custom widget, like something that inherits from QLabel for example, that has this indicator, and then use QTableWidget::setCellWidget() to insert it where I want in the table. Am I missing some other method?
>
> Other (possibly) important notes:
> - the table is read-only, so I don't need to worry about making the QTableWidgetItem editable, so that's why my approach above suggest QLabel, not QLineEdit
> - If there is a way to do it without a custom widget, I'd like to be able to do it via a custom Role (i.e. QTableWidgetItem::setData(myCommentRole, bool commentEnabled) ) if possible, but that's not really critical
>
> Sean
>
>
>
> This message has been scanned for malware by Forcepoint. www.forcepoint.com
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list