#include <QApplication>
#include <QListView>
#include <QTableView>
#include <QAbstractItemDelegate>
#include <QStandardItemModel>
#include <QPainter>
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();
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);
}
qDebug("in sizeHint()");
exit(1);
//return QSize(200, 82); // very dumb value
}
};
int main(int argc, char **argv){
//QListView lv;
model.setRowCount(2);
model.setColumnCount(1);
model.
setData(model.
index(0,
0),
QPixmap("/usr/share/icons/crystalsvg/32x32/actions/filenew.png"), Qt
::DecorationRole);
model.setData(model.index(0, 0), "Some wonderful text which is long enough to cover more than one row");
model.
setData(model.
index(1,
0),
QPixmap("/usr/share/icons/crystalsvg/32x32/actions/exit.png"), Qt
::DecorationRole);
model.setData(model.index(1, 0), "Some wonderful text");
lv.setModel(&model);
lv.setItemDelegate(new PluginDelegate(&lv));
lv.setAlternatingRowColors(true);
lv.show();
return app.exec();
}
#include <QApplication>
#include <QListView>
#include <QTableView>
#include <QAbstractItemDelegate>
#include <QStandardItemModel>
#include <QPainter>
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();
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);
}
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
qDebug("in sizeHint()");
exit(1);
//return QSize(200, 82); // very dumb value
}
};
int main(int argc, char **argv){
QApplication app(argc, argv);
//QListView lv;
QTableView lv;
QStandardItemModel model;
model.setRowCount(2);
model.setColumnCount(1);
model.setData(model.index(0, 0), QPixmap("/usr/share/icons/crystalsvg/32x32/actions/filenew.png"), Qt::DecorationRole);
model.setData(model.index(0, 0), "Some wonderful text which is long enough to cover more than one row");
model.setData(model.index(1, 0), QPixmap("/usr/share/icons/crystalsvg/32x32/actions/exit.png"), Qt::DecorationRole);
model.setData(model.index(1, 0), "Some wonderful text");
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