public:
if(option.
state & QStyle::State_Selected){ painter
->fillRect
(option.
rect, option.
palette.
color(QPalette::Highlight));
}
QIcon ic
= QIcon(qvariant_cast<QPixmap>
(index.
data(Qt
::DecorationRole)));
QString txt
= index.
data(Qt
::DisplayRole).
toString();
QString txt1
= index.
data(Qt
::DisplayRole).
toString();
QRect r
= option.
rect.
adjusted(2,
2,
-2,
-2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
r = r.adjusted(r.height()+20, 0, 0, 0);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
r = r.adjusted(r.height()+20+50, 0, 0, 0);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, txt1, &r);
}
return QSize(200,
52);
// very dumb value }
};
int main(int argc, char *argv[])
{
model.setRowCount(5);
model.setColumnCount(2);
model.
setData(model.
index(0,
0),
QPixmap(":/butterfly.png"), Qt
::DecorationRole);
model.setData(model.index(0, 0), "frist");
model.setData(model.index(0, 0), "Time");
lv.setModel(&model);
lv.setItemDelegate(new PluginDelegate(&lv));
lv.setAlternatingRowColors(true);
lv.show();
return app.exec();
}
class PluginDelegate : public QAbstractItemDelegate {
public:
PluginDelegate(QObject *parent=0) : QAbstractItemDelegate(parent){}
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
if(option.state & QStyle::State_Selected){
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
QString txt = index.data(Qt::DisplayRole).toString();
QString txt1 = index.data(Qt::DisplayRole).toString();
QRect r = option.rect.adjusted(2, 2, -2, -2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
r = r.adjusted(r.height()+20, 0, 0, 0);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
r = r.adjusted(r.height()+20+50, 0, 0, 0);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, txt1, &r);
}
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
return QSize(200, 52); // very dumb value
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QListView lv;
QStandardItemModel model;
model.setRowCount(5);
model.setColumnCount(2);
model.setData(model.index(0, 0), QPixmap(":/butterfly.png"), Qt::DecorationRole);
model.setData(model.index(0, 0), "frist");
model.setData(model.index(0, 0), "Time");
lv.setModel(&model);
lv.setItemDelegate(new PluginDelegate(&lv));
lv.setAlternatingRowColors(true);
lv.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks