I know this has been asked many times but it hasn't been fully answered. I have also googled for quite some time now but haven't found anything helpfull yet.
So basically from the documentation I understand I need an Item Delegate to personalize the preview icon. Thats ok. So I did this test:
class PixmapPreviewDelegate : public QStyledItemDelegate
{
public:
PixmapPreviewDelegate
(QObject *parent
= 0) : QStyledItemDelegate(parent)
{
}
painter->drawText(
option.rect,
displayText
(index.
data(),
QLocale::system()),
option.displayAlignment
);
auxOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
auxOption.rect.setWidth(auxOption.rect.height());
painter
->drawPixmap
(auxOption.
rect,
QPixmap(":images/test.png"));
}
};
class PixmapPreviewDelegate : public QStyledItemDelegate
{
public:
PixmapPreviewDelegate(QObject *parent = 0) :
QStyledItemDelegate(parent)
{
}
void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
painter->drawText(
option.rect,
displayText(index.data(), QLocale::system()),
option.displayAlignment
);
QStyleOptionViewItem auxOpcion = option;
auxOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
auxOption.rect.setWidth(auxOption.rect.height());
painter->drawPixmap(auxOption.rect, QPixmap(":images/test.png"));
}
};
To copy to clipboard, switch view to plain text mode
That worked ok. But I found I had lost all default styles (files where not highlighted when selected), which was unacceptable. Secondly, since the index data has only the file name I couldn't use it in the Pixmap constructor. So I need to constantly tell the delegate the current folder the user is seeing? Isn't this just crazy?
Then I thought I could just change the data's Qt:: DecorationRole in order to change the icon, but didn't know how to do this?
This doesn't seem to work:
Q_UNUSED(widget);
qDebug() << "setModelData";
item
->setData
(index,
QIcon(":images/test.png"), Qt
::DecorationRole);
}
void setModelData(QWidget * widget, QAbstractItemModel* item, const ModelIndex& index ) const {
Q_UNUSED(widget);
qDebug() << "setModelData";
item->setData(index, QIcon(":images/test.png"), Qt::DecorationRole);
}
To copy to clipboard, switch view to plain text mode
I'm really stuck with this.
Thanks for any help.
Bookmarks