Results 1 to 5 of 5

Thread: Painting an image on QItemDelegates

  1. #1
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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.)

    Qt Code:
    1. #include <QApplication>
    2. #include <QTableWidget>
    3. #include <QImage>
    4. #include <QItemDelegate>
    5. #include <QPainter>
    6.  
    7. const int num_images = 10;
    8. const int img_width = 400;
    9. const int img_height = 400;
    10.  
    11. class Delegate : public QItemDelegate {
    12. public:
    13. Delegate(QObject *parent=0) : QItemDelegate(parent){
    14. QImage newImage("/path/to/image.jpg");
    15. m_image = newImage.copy();
    16. }
    17.  
    18. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    19. QItemDelegate::paint(painter, option, index);
    20.  
    21. painter->drawImage(0,0, m_image);
    22. //painter->setPen (QPen (Qt::red, 3));
    23. //painter->drawEllipse(option.rect);
    24. }
    25.  
    26. private:
    27. QImage m_image;
    28. };
    29.  
    30. int main(int argc, char **argv){
    31. QApplication app(argc, argv);
    32.  
    33. QTableWidget tableWidget(1, num_images);
    34. for (int i = 0; i < num_images; i++) {
    35. tableWidget.setColumnWidth(i, img_width);
    36. }
    37. tableWidget.setRowHeight(0, img_height);
    38. tableWidget.setSelectionMode(QAbstractItemView::NoSelection);
    39. tableWidget.setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    40.  
    41. tableWidget.setItemDelegate(new Delegate(&tableWidget));
    42. tableWidget.show();
    43.  
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Painting an image on QItemDelegates

    You should draw within "option.rect" instead of point (0,0).
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    sprek (10th June 2008)

  4. #3
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Painting an image on QItemDelegates

    Thanks! using painter->drawImage(option.rect.x(),option.rect.y(), m_image); works.

  5. #4
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtGui>
    6.  
    7. class Delegate : public QItemDelegate {
    8. public:
    9. Delegate(QObject *parent=0) : QItemDelegate(parent){ }
    10.  
    11. void pushImage (const QImage &image)
    12. {
    13. imageVector.push_back(image);
    14. }
    15.  
    16. void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    17. QItemDelegate::paint(painter, option, index);
    18.  
    19. if (index.column() < imageVector.size()) {
    20. painter->drawImage(option.rect.x(),option.rect.y(),imageVector.at(index.column()));
    21. }
    22. }
    23.  
    24. private:
    25. QVector <QImage> imageVector;
    26. };
    27.  
    28. class MainWindow : public QMainWindow
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. MainWindow(QWidget *parent = 0);
    34. ~MainWindow();
    35.  
    36. private slots:
    37. void add_image();
    38.  
    39. private:
    40. QTableWidget *tableWidget;
    41. QPushButton *button;
    42. Delegate *imageDelegate;
    43. };
    44.  
    45. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp (need to replace /path/to/image with a valid path)
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QtGui>
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. QWidget *widget = new QWidget;
    8. setCentralWidget(widget);
    9.  
    10. tableWidget = new QTableWidget;
    11.  
    12. tableWidget->setColumnCount(7);
    13. tableWidget->setRowCount(1);
    14. imageDelegate = new Delegate(tableWidget);
    15. tableWidget->setItemDelegate(imageDelegate);
    16. tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
    17. tableWidget->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    18.  
    19. for (int i = 0; i < tableWidget->columnCount(); i++) {
    20. tableWidget->setColumnWidth(i,400);
    21. }
    22. tableWidget->setRowHeight(0,400);
    23.  
    24. button = new QPushButton;
    25.  
    26.  
    27. QVBoxLayout *layout = new QVBoxLayout;
    28. layout->addWidget(tableWidget);
    29. layout->addWidget(button);
    30. widget->setLayout(layout);
    31.  
    32. setMinimumSize(160,160);
    33. resize(1000, 800);
    34.  
    35. QObject::connect(button, SIGNAL(clicked()), this, SLOT(add_image()));
    36.  
    37. QTimer *timer = new QTimer(this);
    38. connect(timer, SIGNAL(timeout()), this, SLOT(add_image()));
    39. timer->start(1000);
    40. }
    41.  
    42. MainWindow::~MainWindow()
    43. {
    44.  
    45. }
    46.  
    47. void MainWindow::add_image()
    48. {
    49. QImage newImage("/path/to/image");
    50. imageDelegate->pushImage(newImage);
    51. tableWidget->repaint();
    52. tableWidget->update();
    53. tableWidget->setFocus();
    54. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

Similar Threads

  1. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 03:10
  2. Help needed handling image data
    By toratora in forum General Programming
    Replies: 2
    Last Post: 11th May 2007, 10:24
  3. custom widgets painting image formats
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2007, 11:33
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 17:36
  5. Trouble with image painting (QPainter + QImage)
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 21:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.