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();
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();
To copy to clipboard, switch view to plain text mode
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)
}
QVariant ImageModel::headerData(int /* section */, Qt::Orientation /* orientation */, int role) const
{
if (role == Qt::SizeHintRole)
return QSize(1, 1);
return QVariant();
}
To copy to clipboard, switch view to plain text mode
And the delegate looks like that (header file):
#ifndef IMAGEDELEGATE_H
#define IMAGEDELEGATE_H
#include <QAbstractItemDelegate>
#include <QSize>
#include <QPixmap>
#include <QPainter>
{
public:
ImageDelegate
(QObject * parent
= 0);
};
#endif // IMAGEDELEGATE_H
#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
To copy to clipboard, switch view to plain text mode
and cpp file:
#include "imagedelegate.h"
ImageDelegate
::ImageDelegate(QObject * parent
){
}
{
}
{
int data = index.data(Qt::DisplayRole).toInt();
switch (data)
{
case 0:
{
painter->drawPixmap(option.rect, pm);
return;
}
case 1:
{
painter->drawPixmap(option.rect, pm);
return;
}
case 2:
{
painter->drawPixmap(option.rect, pm);
return;
}
}
}
#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;
}
}
}
To copy to clipboard, switch view to plain text mode
the result you can see in attachment. Also take closer look to Pixelator example in Qt Examples & Demos.
Bookmarks