Hi comunity,

I need a QListWidget with check boxes in it, I used the QItemDelegate class and I reimplemented the functions paint(), sizeHint() and drawCheck(), I did it because I want to change the aligment of the icon, text and check box indicator, after doing it the listWidget was painted as I want, but the problem appear when I select an item and press space bar to select it up, the check box of the item doesn't get checked, the one who do it is the check box of the next item, and if I click on a check box indicator then it doesn't change the state(checked, unchecked), however, if I select an item and after that I move the cursor out of the listWidget and press space bar then the check box of the selected item gets checked, I sopouse I skip some validation, connection or function's reimplementation, but this is my first time using QItemDelegate so I will appreciate any help.

Thanks very much.

Here is my code:

itemdelegate.h
Qt Code:
  1. #include <QItemDelegate>
  2.  
  3. class ItemDelegate : public QItemDelegate
  4. {
  5. Q_OBJECT
  6. public:
  7. explicit ItemDelegate(QObject *parent = 0);
  8.  
  9. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  10. void drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const;
  11. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
  12.  
  13. signals:
  14.  
  15. public slots:
  16.  
  17. };
To copy to clipboard, switch view to plain text mode 

itemdelegate.cpp

Qt Code:
  1. #include "itemdelegate.h"
  2. #include <QPainter>
  3. #include <QApplication>
  4.  
  5. ItemDelegate::ItemDelegate(QObject *parent) :
  6. QItemDelegate(parent)
  7. {
  8. }
  9.  
  10. void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  11. {
  12. QRect r = option.rect;
  13.  
  14. if(option.state & QStyle::State_Selected)
  15. painter->fillRect(r, QColor::fromRgb(176, 222, 241, 100));
  16.  
  17. bool checked = index.data(Qt::CheckStateRole).toBool();
  18. QString text = index.data(Qt::DisplayRole).toString();
  19. QIcon icon = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
  20.  
  21. r.setHeight(r.height() - 20);
  22. icon.paint(painter, r, Qt::AlignHCenter | Qt::AlignBottom);
  23.  
  24. QRect textRect = r;
  25. textRect.setTop(r.bottom());
  26. textRect.setHeight(19);
  27. textRect.setWidth(r.width() - 20);
  28. textRect.setX(r.x() + 20);
  29. painter->setPen(QPen(Qt::black));
  30. painter->drawText(textRect.left(), textRect.top(), textRect.width(), textRect.height(),
  31. Qt::AlignBottom | Qt::TextWordWrap, text, &r);
  32.  
  33. QRect checkRect = textRect;
  34. checkRect.setX(checkRect.x() - 20);
  35. checkRect.setWidth(20);
  36. checkRect.setY(checkRect.y() + 6);
  37. this->drawCheck(painter, option, checkRect, (checked) ? Qt::Checked : Qt::Unchecked);
  38.  
  39. r = option.rect;
  40. painter->setPen(QPen(Qt::gray));
  41. painter->drawRect(option.rect);
  42. }
  43.  
  44. void ItemDelegate::drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
  45. {
  46. if(!rect.isValid())
  47. return;
  48.  
  49. QStyleOptionViewItem opt(option);
  50. opt.rect = rect;
  51.  
  52. if(state == Qt::Unchecked)
  53. opt.state |= QStyle::State_Off;
  54. else
  55. opt.state |= QStyle::State_On;
  56.  
  57. QStyle *style = QApplication::style();
  58. style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, painter);
  59. }
  60.  
  61. QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
  62. {
  63. Q_UNUSED(option);
  64. Q_UNUSED(index);
  65. return QSize(130, 145);
  66. }
To copy to clipboard, switch view to plain text mode 

and here is where I populate the listWidget

Qt Code:
  1. for(int i = 0; i < photosList.count(); ++i) {
  2. item = new QListWidgetItem();
  3. item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
  4. item->setCheckState(Qt::Unchecked);
  5.  
  6. QImageReader reader(photosDir.absolutePath() + "/" + photosList.at(i));
  7. QSize size = reader.size();
  8. reader.setQuality(reader.quality() / 2);
  9.  
  10. reader.setScaledSize(size);
  11.  
  12. item->setData(Qt::DisplayRole, photosList.at(i));
  13. item->setData(Qt::DecorationRole, QPixmap::fromImage(reader.read()));
  14.  
  15. ui->previewsList->addItem(item);
  16. qApp->processEvents();
  17. }
To copy to clipboard, switch view to plain text mode