1 Attachment(s)
Painting an image on QItemDelegates
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-p...dget-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.)
Code:
#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;
public:
QImage newImage
("/path/to/image.jpg");
m_image = newImage.copy();
}
painter->drawImage(0,0, m_image);
//painter->setPen (QPen (Qt::red, 3));
//painter->drawEllipse(option.rect);
}
private:
};
int main(int argc, char **argv){
for (int i = 0; i < num_images; i++) {
tableWidget.setColumnWidth(i, img_width);
}
tableWidget.setRowHeight(0, img_height);
tableWidget.setItemDelegate(new Delegate(&tableWidget));
tableWidget.show();
return app.exec();
}
Re: Painting an image on QItemDelegates
You should draw within "option.rect" instead of point (0,0).
Re: Painting an image on QItemDelegates
Thanks! using painter->drawImage(option.rect.x(),option.rect.y(), m_image); works.
3 Attachment(s)
Re: Painting an image on QItemDelegates
Sorry, I'm stuck again. This time I can't get the QTableWidget to repaint.
The program below demonstrates the trouble I've been having. Originally I had it so that whenever the button is clicked, a new image will get added to my tableWidget. In order to get the image to display each time, I had to call tableWidget->update() and tableWidget->setFocus().
However, if I add images to my tableWidget from a timer (which it's doing now), any combination of update(), repaint(), and setFocus() has no effect. It only repaints when I scroll the table or click on a cell. Any help would be much appreciated. Thanks!
main.cpp
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
public:
void pushImage
(const QImage &image
) {
imageVector.push_back(image);
}
if (index.column() < imageVector.size()) {
painter->drawImage(option.rect.x(),option.rect.y(),imageVector.at(index.column()));
}
}
private:
};
{
Q_OBJECT
public:
~MainWindow();
private slots:
void add_image();
private:
Delegate *imageDelegate;
};
#endif // MAINWINDOW_H
mainwindow.cpp (need to replace /path/to/image with a valid path)
Code:
#include "mainwindow.h"
#include <QtGui>
MainWindow
::MainWindow(QWidget *parent
){
setCentralWidget(widget);
tableWidget->setColumnCount(7);
tableWidget->setRowCount(1);
imageDelegate = new Delegate(tableWidget);
tableWidget->setItemDelegate(imageDelegate);
for (int i = 0; i < tableWidget->columnCount(); i++) {
tableWidget->setColumnWidth(i,400);
}
tableWidget->setRowHeight(0,400);
layout->addWidget(tableWidget);
layout->addWidget(button);
widget->setLayout(layout);
setMinimumSize(160,160);
resize(1000, 800);
QObject::connect(button,
SIGNAL(clicked
()),
this,
SLOT(add_image
()));
connect(timer, SIGNAL(timeout()), this, SLOT(add_image()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
}
void MainWindow::add_image()
{
QImage newImage
("/path/to/image");
imageDelegate->pushImage(newImage);
tableWidget->repaint();
tableWidget->update();
tableWidget->setFocus();
}
Re: Painting an image on QItemDelegates
The delegate is only used to display data that is contained in the model (be it a real one or a hidden one when using convenience widgets). Storing any data within the delegate itself doesn't make much sense. it's like you wanted to store different coulours of paint on a brush for future use :) Treat the delegate as a brush that is used many many times for each and every item that gets drawn. Store the data in the model and use the delegate to fetch the data from the model and render it on the view. I don't know what is your exact usecase but doing it the way you are using now will simply not work.