Results 1 to 2 of 2

Thread: QStyledItemDelegate events are not called

  1. #1
    Join Date
    Oct 2015
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QStyledItemDelegate events are not called

    Hi everyone.

    This is my first post in this comunity and hopefully not the last.

    My problem is the next, I've created a custom delegated for a QComboBox but I don't see the items, I mean, when I click in the combo I can see deployed items but not the information(a image with a text and two more images). Debugging I've noticed that the paint event never is called. The curious thing is that the info in the items exists because when I click one of them, the info shows in the combo. My code:

    delegates.h
    Qt Code:
    1. class ComboUpDownDelegate : public QStyledItemDelegate {
    2. Q_OBJECT
    3.  
    4. public:
    5. static const int SESSION_DATA_ROLE;
    6. static const int ICON1_ROLE;
    7. static const int ICON2_ROLE;
    8. static const int ICON3_ROLE;
    9.  
    10. static const char* COMBO_TYPE;
    11. static const QString COMBO_ICON_TEXT;
    12. static const QString COMBO_ICON_TEXT_ICONS;
    13. static const QString COMBO_ICON_RICHTEXT_COUNTER;
    14.  
    15. public:
    16. ComboUpDownDelegate(QObject* parent);
    17. ~ComboUpDownDelegate();
    18.  
    19. void initCombo(QComboBox* combo);
    20.  
    21. virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const override;
    22. virtual void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
    23. virtual bool eventFilter(QObject *object, QEvent *event) override;
    24.  
    25. void setTitle(const QString& title);
    26. QString title() const;
    27.  
    28. virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
    29. return nullptr;
    30. }
    31.  
    32. protected:
    33. void drawIconRichTextCounter(QPainter* painter, const CUDSession& data, const QPoint& topLeft, int row, int width) const;
    34.  
    35. private:
    36. QString m_title;
    37. QPixmap m_icon1, m_icon2, m_icon3;
    38. };
    To copy to clipboard, switch view to plain text mode 


    delegates.cpp
    Qt Code:
    1. namespace {
    2. QPoint drawText(QPainter* painter, const QPoint& point, const QString& text, const QFont& font) {
    3. painter->setFont(font);
    4.  
    5. QFontMetrics fm(font);
    6. const int w = fm.width(text);
    7. painter->drawText(point, text);
    8.  
    9. return QPoint(point.x() + w, point.y());
    10. }
    11. }
    12.  
    13. const int ComboUpDownDelegate::SESSION_DATA_ROLE = Qt::UserRole + 1;
    14. const int ComboUpDownDelegate::ICON1_ROLE = Qt::UserRole + 1;
    15. const int ComboUpDownDelegate::ICON2_ROLE = Qt::UserRole + 2;
    16. const int ComboUpDownDelegate::ICON3_ROLE = Qt::UserRole + 3;
    17.  
    18. const char* ComboUpDownDelegate::COMBO_TYPE = "combo_type";
    19. const QString ComboUpDownDelegate::COMBO_ICON_TEXT = "icon_text";
    20. const QString ComboUpDownDelegate::COMBO_ICON_TEXT_ICONS = "icon_text_icons";
    21. const QString ComboUpDownDelegate::COMBO_ICON_RICHTEXT_COUNTER = "icon_richtext_counter";
    22.  
    23. ComboUpDownDelegate::ComboUpDownDelegate(QObject* parent)
    24. : QStyledItemDelegate(parent) {
    25. initCombo(qobject_cast<QComboBox*>(parent));
    26.  
    27. // Icons
    28. m_icon1 = Customisation::getImage("time", QSize(16, 16), QColor("#C8C8C8"));
    29. m_icon2 = Customisation::getImage("warning", QSize(16, 16), QColor("#C8C8C8"));
    30. m_icon3 = Customisation::getImage("warning", QSize(16, 16), QColor("#FF0000"));
    31. }
    32.  
    33. ComboUpDownDelegate::~ComboUpDownDelegate() {
    34. }
    35.  
    36. void ComboUpDownDelegate::initCombo(QComboBox* combo) {
    37. if (combo != nullptr) {
    38. combo->view()->setEditTriggers(QAbstractItemView::CurrentChanged);
    39. combo->installEventFilter(this);
    40. }
    41. }
    42.  
    43. QSize ComboUpDownDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const {
    44. auto combo = static_cast<QComboBox*>(this->parent());
    45.  
    46. const auto text = index.data(Qt::DisplayRole).toString();
    47. const auto style = QApplication::style();
    48.  
    49. opt.text = text;
    50. opt.rect = option.rect;
    51. opt.icon = index.data(Qt::DecorationRole).value<QIcon>();
    52. if (combo != nullptr) {
    53. opt.iconSize = combo->iconSize();
    54. }
    55.  
    56. return style->sizeFromContents(QStyle::CT_CheckBox, &opt, QSize());
    57. //return style->sizeFromContents(QStyle::CT_PushButton, &opt, QSize());
    58. }
    59.  
    60. void ComboUpDownDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
    61. QComboBox* combo = static_cast<QComboBox*>(this->parent());
    62.  
    63. // Basic style
    64. QStyleOptionViewItemV4 modifiedOption(option);
    65. modifiedOption.text.clear();
    66. modifiedOption.icon = QIcon();
    67. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &modifiedOption, painter);
    68.  
    69. const auto topLeft = option.rect.topLeft();
    70. const int margin = 3;
    71. const int fontHeight = QFontMetrics(painter->font()).height();
    72.  
    73. painter->setPen(combo->palette().color(QPalette::Text));
    74.  
    75. if (combo->property(COMBO_TYPE) == COMBO_ICON_TEXT)
    76. {
    77. const bool state1 = index.data(ICON1_ROLE).toBool();
    78. QPixmap icon1(state1 ? m_icon1 : m_icon2);
    79. painter->drawPixmap(margin, topLeft.y() + margin, icon1);
    80.  
    81. const QString text = index.data(Qt::DisplayRole).toString();
    82. painter->drawText(icon1.width() + 2 * margin, topLeft.y() + fontHeight + 3, text);
    83.  
    84. const bool state2 = index.data(ICON2_ROLE).toBool();
    85. QPixmap icon2(state2 ? m_icon1 : m_icon2);
    86. int w = option.rect.width() - icon2.width() - margin;
    87. painter->drawPixmap(w, topLeft.y() + margin, icon2);
    88. }
    89. else if (combo->property(COMBO_TYPE) == COMBO_ICON_TEXT_ICONS)
    90. {
    91. const bool state1 = index.data(ICON1_ROLE).toBool();
    92. QPixmap icon1(state1 ? m_icon1 : m_icon2);
    93. painter->drawPixmap(margin, topLeft.y() + margin, icon1);
    94.  
    95. const QString text = index.data(Qt::DisplayRole).toString();
    96. painter->drawText(icon1.width() + 2 * margin, topLeft.y() + fontHeight + 3, text);
    97.  
    98. const bool state3 = index.data(ICON3_ROLE).toBool();
    99. QPixmap icon3(state3 ? m_icon1 : m_icon2);
    100. int w = option.rect.width() - icon3.width() - margin;
    101. painter->drawPixmap(w, topLeft.y() + margin, icon3);
    102.  
    103. const bool state2 = index.data(ICON2_ROLE).toBool();
    104. QPixmap icon2(state2 ? m_icon1 : m_icon2);
    105. w -= icon2.width();
    106. painter->drawPixmap(w, topLeft.y() + margin, icon2);
    107. }
    108. else if (combo->property(COMBO_TYPE) == COMBO_ICON_RICHTEXT_COUNTER)
    109. {
    110. const CUDSession data = index.data(SESSION_DATA_ROLE).value<CUDSession>();
    111. drawIconRichTextCounter(painter, data, topLeft, index.row(), option.rect.width());
    112. }
    113. }
    114.  
    115. void ComboUpDownDelegate::drawIconRichTextCounter(QPainter* painter, const CUDSession& data, const QPoint& topLeft, int row, int width) const {
    116. const int margin = 15;
    117.  
    118. QPixmap icon1(m_icon1);
    119. painter->drawPixmap(margin, topLeft.y() + margin, icon1);
    120.  
    121. QFont normalFont(painter->font());
    122. QFont boldFont(normalFont);
    123. boldFont.setBold(true);
    124. QFontMetrics fm(normalFont);
    125.  
    126. QPoint p(topLeft + QPoint(icon1.width() + 1.5 * margin, fm.height() + 11));
    127. if (row == 0)
    128. {
    129. p = drawText(painter, p, QString("ALL SESSIONS"), boldFont);
    130. }
    131. else
    132. {
    133. QString strRow = QString::number(row);
    134. if(strRow.length() < 2)
    135. {
    136. strRow = "0" + strRow;
    137. }
    138. p = drawText(painter, p, QString("SESSION %1").arg(strRow), boldFont);
    139. painter->setPen(QColor("#C8C8C8"));
    140. p = drawText(painter, p, QString(" | "), normalFont);
    141. p = drawText(painter, p, QString("START:"), boldFont);
    142. p = drawText(painter, p, QString(" %1 - ").arg(data.StartStr()), normalFont);
    143. p = drawText(painter, p, QString("END:"), boldFont);
    144. if(data.End().isValid())
    145. {
    146. p = drawText(painter, p, QString(" %1").arg(data.EndStr()), normalFont);
    147. }
    148. else
    149. {
    150. p = drawText(painter, p, QString(" %1").arg(tr("Running")), normalFont);
    151. }
    152.  
    153. const QString errorCounter(QString::number(data.Errors()));
    154. p.setX(width - fm.width("888") - margin);
    155. painter->drawText(p, errorCounter);
    156.  
    157. QPixmap icon2(data.Errors() > 0 ? m_icon3 : m_icon2);
    158. painter->drawPixmap(p.x() - icon2.width() - 3, topLeft.y() + margin, icon2);
    159. }
    160.  
    161. }
    162.  
    163. bool ComboUpDownDelegate::eventFilter(QObject *object, QEvent *event) {
    164. auto combo = static_cast<QComboBox*>(object);
    165.  
    166. if (combo != nullptr && object == this->parent() && event->type() == QEvent::Paint) {
    167. QStylePainter painter(combo);
    168.  
    169. painter.setPen(combo->palette().color(QPalette::Text));
    170.  
    171. opt.initFrom(combo);
    172. opt.editable = combo->isEditable();
    173. opt.frame = combo->hasFrame();
    174. if (combo->hasFocus() && !opt.editable) {
    175. opt.state |= QStyle::State_Selected;
    176. }
    177. opt.subControls = QStyle::SC_All;
    178.  
    179. if (m_title.isEmpty()) {
    180. opt.currentText = combo->currentText();
    181. } else {
    182. opt.currentText = m_title.arg(combo->count());
    183. }
    184. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    185. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
    186.  
    187. if (combo->property(COMBO_TYPE) == COMBO_ICON_RICHTEXT_COUNTER) {
    188. drawIconRichTextCounter(&painter, combo->currentData(SESSION_DATA_ROLE).value<CUDSession>(),
    189. QPoint(0, 0), combo->currentIndex(), combo->width() - 16);
    190. }
    191.  
    192. return true;
    193. }
    194. return QStyledItemDelegate::eventFilter(object, event);
    195. }
    196.  
    197. void ComboUpDownDelegate::setTitle(const QString& title) {
    198. m_title = title;
    199. }
    200.  
    201. QString ComboUpDownDelegate::title() const {
    202. return m_title;
    203. }
    To copy to clipboard, switch view to plain text mode 

    Where I create the combo:
    Qt Code:
    1. auto comboSesiones = new QComboBox();
    2. comboSesiones->setProperty(ComboUpDownDelegate::COMBO_TYPE, ComboUpDownDelegate::COMBO_ICON_RICHTEXT_COUNTER);
    3. comboSesiones->setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy::AdjustToContents);
    4. comboSesiones->setMinimumWidth(525);
    5. auto delegate = new ComboUpDownDelegate(comboSesiones);
    6. comboSesiones->setItemDelegate(delegate);
    7. connect(comboSesiones, SIGNAL(currentIndexChanged(int)), SLOT(onSesionCurrentChanged(int)));
    8. comboSesiones->setVisible(false);
    9. comboSesiones->addItem("");
    10. int index = comboSesiones->count() - 1;
    11. comboSesiones->setItemData(index, QVariant(), ComboUpDownDelegate::SESSION_DATA_ROLE);
    To copy to clipboard, switch view to plain text mode 

    Thank you very much.
    Attached Images Attached Images

  2. #2
    Join Date
    Oct 2015
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QStyledItemDelegate events are not called

    ¿Any idea?

Similar Threads

  1. QStyledItemDelegate crash
    By folibis in forum Qt Programming
    Replies: 6
    Last Post: 31st October 2013, 08:03
  2. Custom QStyledItemDelegate paint function never called
    By mattsnowboard in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2011, 01:57
  3. Custom QStyledItemDelegate
    By Berryblue031 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2011, 10:32
  4. QGraphicsItem no mouse events called
    By munna in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2009, 14:43
  5. wrong virtual function called at some events on MacOS?
    By akos.maroy in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2009, 11:06

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.