PDA

View Full Version : [Resolved] How can I add a column with Image into the model



lovehouye
12th February 2009, 02:17
Hi everyone,
I have rewrite the QDirModel by myslef. I want to add a column to show some images.

In the QDirModel, there is only 'Name', 'Size', 'Type', 'Date Modified'. And all of them are QString type.


QString name(const QModelIndex &index) const;
QString size(const QModelIndex &index) const;
QString type(const QModelIndex &index) const;
QString time(const QModelIndex &index) const;

case 0: return d->name(index);
case 1: return d->size(index);
case 2: return d->type(index);
case 3: return d->time(index);



So, I want to know what type can I set that it can show the images in the additional column. I have tried 'QPixmap' and 'QIcon', but they are not work.

What can I do? I need your help. Thanks.

nifei
12th February 2009, 02:46
QVariant QDirModel::data(const QModelIndex &index, int role) const
{
Q_D(const QDirModel);
if (!d->indexValid(index))
return QVariant();

if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column()) {
case 0: return d->name(index);
case 1: return d->size(index);
case 2: return d->type(index);
case 3: return d->time(index);
// you can return icons if the role == Qt::DecorationRole
//or else you can return icons if the role == Qt::DecorationRole and the index is in the column you want to present icons
default:
qWarning("data: invalid display value column %d", index.column());
return QVariant();
}
}

if (index.column() == 0) {
if (role == FileIconRole)
return fileIcon(index);
if (role == FilePathRole)
return filePath(index);
if (role == FileNameRole)
return fileName(index);
}

if (index.column() == 1 && Qt::TextAlignmentRole == role) {
return Qt::AlignRight;
}
return QVariant();
}

lovehouye
12th February 2009, 03:42
QVariant QDirModel::data(const QModelIndex &index, int role) const
{
Q_D(const QDirModel);
if (!d->indexValid(index))
return QVariant();

if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column()) {
case 0: return d->name(index);
case 1: return d->size(index);
case 2: return d->type(index);
case 3: return d->time(index);
// you can return icons if the role == Qt::DecorationRole
//or else you can return icons if the role == Qt::DecorationRole and the index is in the column you want to present icons
default:
qWarning("data: invalid display value column %d", index.column());
return QVariant();
}
}

if (index.column() == 0) {
if (role == FileIconRole)
return fileIcon(index);
if (role == FilePathRole)
return filePath(index);
if (role == FileNameRole)
return fileName(index);
}

if (index.column() == 1 && Qt::TextAlignmentRole == role) {
return Qt::AlignRight;
}
return QVariant();
}
Oh, you're so kind and it works perfect.
Thanks very much.