PDA

View Full Version : Preview Thumbnail of images in Custom QFileDialog



toglia3d
12th October 2010, 22:47
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)
{
}

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"));
}
};

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:


void setModelData(QWidget * widget, QAbstractItemModel* item, const ModelIndex& index ) const {
Q_UNUSED(widget);
qDebug() << "setModelData";
item->setData(index, QIcon(":images/test.png"), Qt::DecorationRole);
}


I'm really stuck with this.

Thanks for any help.

SixDegrees
12th October 2010, 22:54
You could use Qt3's file dialog, which provided the ability to add a preview widget of your own. Much, much simpler than the approach you're taking.

You could also lobby the Trolls to do the right thing and bring this feature back. It's absence is inexcusable, and the Qt file dialog took a giant step backward when it was removed.

Or, you could examine the UI file from the Qt file dialog source and insert your own preview widget into the layout, connecting it to the signals that already exist denoting changing selections. You can do this at runtime, if you like, with the assumption that the layout naming will remain constant in the future. Not a great solution, but better than nothing until the Trolls come to their senses.