Results 1 to 5 of 5

Thread: Images + QTableview + Delegates

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Images + QTableview + Delegates

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Images + QTableview + Delegates

    Here is the mainwindow.cpp fragment (from constructor):

    Qt Code:
    1. ui->tableView->setShowGrid(false);
    2. ui->tableView->horizontalHeader()->hide();
    3. ui->tableView->verticalHeader()->hide();
    4. ui->tableView->horizontalHeader()->setMinimumSectionSize(1);
    5. ui->tableView->verticalHeader()->setMinimumSectionSize(1);
    6. ui->tableView->setModel(new ImageModel(this));
    7. ui->tableView->setItemDelegate(new ImageDelegate(this));
    8. ui->tableView->resizeColumnsToContents();
    9. ui->tableView->resizeRowsToContents();
    To copy to clipboard, switch view to plain text mode 

    ImageModel is returning 0, 1, 2 as data() depending od row and col. It's derived from QAbstractTableModel and has headerData():
    Qt Code:
    1. QVariant ImageModel::headerData(int /* section */, Qt::Orientation /* orientation */, int role) const
    2. {
    3. if (role == Qt::SizeHintRole)
    4. return QSize(1, 1);
    5. return QVariant();
    6. }
    To copy to clipboard, switch view to plain text mode 

    And the delegate looks like that (header file):
    Qt Code:
    1. #ifndef IMAGEDELEGATE_H
    2. #define IMAGEDELEGATE_H
    3.  
    4. #include <QAbstractItemDelegate>
    5. #include <QSize>
    6. #include <QPixmap>
    7. #include <QPainter>
    8.  
    9. class ImageDelegate : public QAbstractItemDelegate
    10. {
    11. public:
    12. ImageDelegate(QObject * parent = 0);
    13.  
    14. void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    15. QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const;
    16. };
    17.  
    18. #endif // IMAGEDELEGATE_H
    To copy to clipboard, switch view to plain text mode 

    and cpp file:

    Qt Code:
    1. #include "imagedelegate.h"
    2.  
    3. ImageDelegate::ImageDelegate(QObject * parent)
    4. {
    5. }
    6.  
    7. QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
    8. {
    9. return QSize(32,32);
    10. }
    11.  
    12. void ImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    13. {
    14. int data = index.data(Qt::DisplayRole).toInt();
    15.  
    16. switch (data)
    17. {
    18. case 0:
    19. {
    20. QPixmap pm(":/img/red");
    21. painter->drawPixmap(option.rect, pm);
    22. return;
    23. }
    24. case 1:
    25. {
    26. QPixmap pm(":/img/green");
    27. painter->drawPixmap(option.rect, pm);
    28. return;
    29. }
    30. case 2:
    31. {
    32. QPixmap pm(":/img/blue");
    33. painter->drawPixmap(option.rect, pm);
    34. return;
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    the result you can see in attachment. Also take closer look to Pixelator example in Qt Examples & Demos.
    Attached Images Attached Images
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    chshlin (3rd November 2010)

  4. #3
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Images + QTableview + Delegates

    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

  5. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Images + QTableview + Delegates

    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.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #5
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Images + QTableview + Delegates

    And the paintEvent() must be the same as in your paint() example for example? Is this correct?

    LG NoRulez

Similar Threads

  1. Different delegates to different Columns of QTableView.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 10:17
  2. import large number of images
    By sriluyarlagadda in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2008, 11:26
  3. QTableView, QSqlQueryModel and delegates
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2008, 13:04

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.