Results 1 to 5 of 5

Thread: Painting an image on QItemDelegates

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Similar Threads

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