sprek
9th June 2008, 20:48
Hello. I've been having some trouble trying to set up a table where each cell contains an image. To demonstrate the problem, I've modified wysota's example code from this thread: http://www.qtcentre.org/forum/f-qt-programming-2/t-fast-painting-scrollable-widget-9296.html
The way it is right now, scrolling the table messes up the image. If I comment out the line "painter->drawImage(0,0, m_image);" and uncomment the two lines below it that draw an ellipse, it behaves fine.
Can someone please tell me what I'm doing wrong? Thanks!
(To run the code you need to replace "/path/to/image.jpg" with a valid path.)
#include <QApplication>
#include <QTableWidget>
#include <QImage>
#include <QItemDelegate>
#include <QPainter>
const int num_images = 10;
const int img_width = 400;
const int img_height = 400;
class Delegate : public QItemDelegate {
public:
Delegate(QObject *parent=0) : QItemDelegate(parent){
QImage newImage("/path/to/image.jpg");
m_image = newImage.copy();
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QItemDelegate::paint(painter, option, index);
painter->drawImage(0,0, m_image);
//painter->setPen (QPen (Qt::red, 3));
//painter->drawEllipse(option.rect);
}
private:
QImage m_image;
};
int main(int argc, char **argv){
QApplication app(argc, argv);
QTableWidget tableWidget(1, num_images);
for (int i = 0; i < num_images; i++) {
tableWidget.setColumnWidth(i, img_width);
}
tableWidget.setRowHeight(0, img_height);
tableWidget.setSelectionMode(QAbstractItemView::No Selection);
tableWidget.setHorizontalScrollMode(QAbstractItemV iew::ScrollPerPixel);
tableWidget.setItemDelegate(new Delegate(&tableWidget));
tableWidget.show();
return app.exec();
}
The way it is right now, scrolling the table messes up the image. If I comment out the line "painter->drawImage(0,0, m_image);" and uncomment the two lines below it that draw an ellipse, it behaves fine.
Can someone please tell me what I'm doing wrong? Thanks!
(To run the code you need to replace "/path/to/image.jpg" with a valid path.)
#include <QApplication>
#include <QTableWidget>
#include <QImage>
#include <QItemDelegate>
#include <QPainter>
const int num_images = 10;
const int img_width = 400;
const int img_height = 400;
class Delegate : public QItemDelegate {
public:
Delegate(QObject *parent=0) : QItemDelegate(parent){
QImage newImage("/path/to/image.jpg");
m_image = newImage.copy();
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QItemDelegate::paint(painter, option, index);
painter->drawImage(0,0, m_image);
//painter->setPen (QPen (Qt::red, 3));
//painter->drawEllipse(option.rect);
}
private:
QImage m_image;
};
int main(int argc, char **argv){
QApplication app(argc, argv);
QTableWidget tableWidget(1, num_images);
for (int i = 0; i < num_images; i++) {
tableWidget.setColumnWidth(i, img_width);
}
tableWidget.setRowHeight(0, img_height);
tableWidget.setSelectionMode(QAbstractItemView::No Selection);
tableWidget.setHorizontalScrollMode(QAbstractItemV iew::ScrollPerPixel);
tableWidget.setItemDelegate(new Delegate(&tableWidget));
tableWidget.show();
return app.exec();
}