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
Printable View
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
Here is the mainwindow.cpp fragment (from constructor):
Code:
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():
Code:
QVariant ImageModel::headerData(int /* section */, Qt::Orientation /* orientation */, int role) const { if (role == Qt::SizeHintRole) }
And the delegate looks like that (header file):
Code:
#ifndef IMAGEDELEGATE_H #define IMAGEDELEGATE_H #include <QAbstractItemDelegate> #include <QSize> #include <QPixmap> #include <QPainter> { public: void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const; }; #endif // IMAGEDELEGATE_H
and cpp file:
Code:
#include "imagedelegate.h" { } { } void ImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { 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; } } }
the result you can see in attachment. Also take closer look to Pixelator example in Qt Examples & Demos.
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
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.
And the paintEvent() must be the same as in your paint() example for example? Is this correct?
LG NoRulez