PDA

View Full Version : Images + QTableview + Delegates



NoRulez
9th February 2009, 13:24
Hey @all,

can anybody help me or can give me an example on how to draw an image into a cell without white spaces on each side using a delegates?

Kind Regards
NoRulez

faldzip
9th February 2009, 19:25
Here is the mainwindow.cpp fragment (from constructor):



ui->tableView->setShowGrid(false);
ui->tableView->horizontalHeader()->hide();
ui->tableView->verticalHeader()->hide();
ui->tableView->horizontalHeader()->setMinimumSectionSize(1);
ui->tableView->verticalHeader()->setMinimumSectionSize(1);
ui->tableView->setModel(new ImageModel(this));
ui->tableView->setItemDelegate(new ImageDelegate(this));
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowsToContents();


ImageModel is returning 0, 1, 2 as data() depending od row and col. It's derived from QAbstractTableModel and has headerData():


QVariant ImageModel::headerData(int /* section */, Qt::Orientation /* orientation */, int role) const
{
if (role == Qt::SizeHintRole)
return QSize(1, 1);
return QVariant();
}


And the delegate looks like that (header file):


#ifndef IMAGEDELEGATE_H
#define IMAGEDELEGATE_H

#include <QAbstractItemDelegate>
#include <QSize>
#include <QPixmap>
#include <QPainter>

class ImageDelegate : public QAbstractItemDelegate
{
public:
ImageDelegate(QObject * parent = 0);

void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const;
};

#endif // IMAGEDELEGATE_H


and cpp file:



#include "imagedelegate.h"

ImageDelegate::ImageDelegate(QObject * parent)
: QAbstractItemDelegate(parent)
{
}

QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
return QSize(32,32);
}

void ImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
int data = index.data(Qt::DisplayRole).toInt();

switch (data)
{
case 0:
{
QPixmap pm(":/img/red");
painter->drawPixmap(option.rect, pm);
return;
}
case 1:
{
QPixmap pm(":/img/green");
painter->drawPixmap(option.rect, pm);
return;
}
case 2:
{
QPixmap pm(":/img/blue");
painter->drawPixmap(option.rect, pm);
return;
}
}
}


the result you can see in attachment. Also take closer look to Pixelator example in Qt Examples & Demos.

NoRulez
9th February 2009, 19:31
Thank you very much, but if I had vertical and horizontal headers, the image on (for example) the vertical header is tiny, how can i made to have a background image on the header?
QHeaderView doesn't have an addPixmap or something similar function. I read that QHeaderView can't be modified with Delegates is it?

Kind Regards
NoRulez

faldzip
9th February 2009, 19:48
Setting delegate for QHeaderView has no effect, so:
1. You can use Qt::BackgroundRole for setting brush used for background painting
2. Or, I'm afraid, you have to subclass QHeaderView and reimplement paintEvent(), as it stands in docs.

NoRulez
9th February 2009, 20:16
And the paintEvent() must be the same as in your paint() example for example? Is this correct?

LG NoRulez