问题
I'm trying to implement a custom paint function for a QStyledItemDelegate subclass (QT4.8.2).
I've reviewed the StarItemDelegate example, among others, and it appears to be pretty straightforward. The delegate is assigned to a column of the table that indicates the record state. Column items are editable, but not user editable. I've implemented the delegate subclass and have proven that it works, but can't seem to get it to draw a simple icon.
The code for the paint function is:
{
painter->save();
QIcon icon(":./opencs.png");
QSize iconsize = option.decorationSize;
painter->drawPixmap(0.0, 0.0, icon.pixmap(iconsize.width(), iconsize.height()));
painter->restore();
}
Right now, I'm just trying to ensure that the paint function works. The icon is already used in other areas of the application, so I know that it works. I don't have a great grasp of QT just yet, so I'm certain it's a fairly straightforward problem to solve, but nothing jumps out at me from the forum posts I've perused or the QT examples I've managed to dig up.
Any thoughts?
回答1:
http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html#details
http://qt-project.org/doc/qt-4.8/qt.html#ItemDataRole-enum
Have you looked into using Qt::DecorationRole
instead?
Just from reading the documentation here:
http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html#subclassing-qstyleditemdelegate
http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html#paint
and your sample code, it looks like you are following the documentation properly...
Have you checked out this example:
http://qt-project.org/doc/qt-4.8/itemviews-stardelegate.html
?
You could also throw in some qDebug
statements in there to make sure that your element is visible/shown, and that it gets to your paint event by putting qDebug() << Q_FUNC_INFO;
at the top of your paint event.
Hope that helps.
来源:https://stackoverflow.com/questions/16714913/drawing-icons-in-a-table-with-qstyleditemdelegate-paint