Results 1 to 4 of 4

Thread: Update the combobox delegate in runtime while the popup is opened

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

    Default Update the combobox delegate in runtime while the popup is opened

    Hi everyone.

    I have a QStyleItemDelegate for my QCombobox where I show an icon with a text and two more icons.
    The items are displayed well but there is an incorrect behaviour when the user has opened the popup of the combobox. The icons change in runtime and when the popup is opened by the user, in spite of doing a "combobox.update" the icons don't refresh. They only refresh when I close the popup and I open it again. A curious thing too happens when I move the mouse on a combobox item because that item refreshes, but only that one.

    ¿Any help? Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Update the combobox delegate in runtime while the popup is opened

    Post the code of your delegate (or other relevant code) so we can have an idea how to help you.

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

    Default Re: Update the combobox delegate in runtime while the popup is opened

    Thanks for your interest. Here is my delegate:
    Qt Code:
    1. ComboUpDownDelegate::ComboUpDownDelegate(QObject* parent): AuniaComboDelegate_Base(parent) {
    2. auto combobox = qobject_cast<QComboBox*>(parent);
    3. initCombo(combobox);
    4.  
    5. // Icons
    6. m_iconClock = Customisation::getImage("time", QSize(16, 16), QColor("#C8C8C8"));
    7. m_iconWarningNoErrors = Customisation::getImage("warning", QSize(16, 16), QColor("#C8C8C8"));
    8. ...
    9. ...more icons
    10. ...
    11. }
    12.  
    13. ComboUpDownDelegate::~ComboUpDownDelegate() {
    14. }
    15.  
    16. void ComboUpDownDelegate::initCombo(QComboBox* combo) {
    17. if (combo != nullptr) {
    18. combo->view()->setEditTriggers(QAbstractItemView::NoEditTriggers);
    19. combo->installEventFilter(this);
    20. }
    21. }
    22.  
    23. QSize ComboUpDownDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const {
    24. auto combo = static_cast<QComboBox*>(this->parent());
    25.  
    26. const auto text = index.data(Qt::DisplayRole).toString();
    27. const auto style = QApplication::style();
    28.  
    29. opt.text = text;
    30. opt.rect = option.rect;
    31. opt.icon = index.data(Qt::DecorationRole).value<QIcon>();
    32. if (combo != nullptr) {
    33. opt.iconSize = combo->iconSize();
    34. }
    35.  
    36. return style->sizeFromContents(QStyle::CT_CheckBox, &opt, QSize());
    37. }
    38.  
    39. QWidget* ComboUpDownDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
    40. const QModelIndex& index) const {
    41. return nullptr;
    42. }
    43.  
    44. void ComboUpDownDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
    45. QComboBox* combo = static_cast<QComboBox*>(this->parent());
    46.  
    47. // Basic style
    48. QStyleOptionViewItemV4 modifiedOption(option);
    49. modifiedOption.text.clear();
    50. modifiedOption.icon = QIcon();
    51. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &modifiedOption, painter);
    52.  
    53. const auto topLeft = option.rect.topLeft();
    54. const int margin = 3;
    55.  
    56. painter->setPen(combo->palette().color(QPalette::Text));
    57.  
    58. // Icon for the state of the device
    59. const bool state1 = index.data(ICON1_ROLE).toBool();
    60. QPixmap icon1(state1 ? m_iconDeviceConnected : m_iconDeviceDisconnected);
    61. painter->drawPixmap(margin, topLeft.y() + margin + 4, icon1);
    62.  
    63. // Text to show
    64. QFont normalFont(painter->font());
    65. normalFont.setBold(false);
    66. QFont boldFont(normalFont);
    67. boldFont.setBold(true);
    68. QFontMetrics fm(normalFont);
    69. painter->setPen(QColor("#7C7C7C"));
    70.  
    71. QPoint p(topLeft + QPoint(icon1.width() + 2.5 * margin, fm.height() + 6));
    72. const QString deviceText = index.data(Qt::DisplayRole).toString();
    73. const QString UTText = " - " + index.data(UT_ROLE).toString();
    74. p = drawText(painter, p, deviceText, boldFont);
    75. p.setX(p.x() + 2);
    76. p = drawText(painter, p, UTText, normalFont);
    77.  
    78. // Icon for the state of the last execution
    79. const int state3 = index.data(ICON3_ROLE).toInt();
    80. QPixmap icon3;
    81. QPixmap noIcon3;
    82. switch (state3) {
    83. case 1:
    84. icon3 = m_iconExecutionNoErrors;
    85. break;
    86. case 2:
    87. icon3 = m_iconExecutionErrors;
    88. break;
    89. case 3:
    90. icon3 = m_iconRunning;
    91. break;
    92. default:
    93. noIcon3 = m_iconExecutionNoErrors;
    94. break;
    95. }
    96. int w;
    97. if(!icon3.isNull())
    98. {
    99. w = option.rect.width() - icon3.width() - margin;
    100. painter->drawPixmap(w, topLeft.y() + margin + 8, icon3);
    101. }
    102. else
    103. {
    104. w = option.rect.width() - noIcon3.width() - margin;
    105. }
    106.  
    107. // Icon to filter the traces of the devices
    108. const int state2 = index.data(ICON2_ROLE).toInt();
    109. QPixmap icon2;
    110. QPixmap noIcon2;
    111. switch (state2) {
    112. case 1:
    113. icon2 = m_iconEye;
    114. break;
    115. case 2:
    116. icon2 = m_iconStrikethroughEye;
    117. break;
    118. default:
    119. noIcon2 = m_iconEye;
    120. break;
    121. }
    122. if(!icon2.isNull())
    123. {
    124. w -= icon2.width();
    125. w -= 8;
    126. painter->drawPixmap(w, topLeft.y() + margin + 8, icon2);
    127. }
    128. }
    129.  
    130. bool ComboUpDownDelegate::eventFilter(QObject *object, QEvent *event) {
    131. auto combo = qobject_cast<QComboBox*>(object);
    132.  
    133. if (combo != nullptr && object == this->parent() && event->type() == QEvent::Paint) {
    134. QStylePainter painter(combo);
    135.  
    136. painter.setPen(combo->palette().color(QPalette::Text));
    137.  
    138. opt.initFrom(combo);
    139. opt.editable = combo->isEditable();
    140. opt.frame = combo->hasFrame();
    141. if (combo->hasFocus() && !opt.editable) {
    142. opt.state |= QStyle::State_Selected;
    143. }
    144. opt.subControls = QStyle::SC_All;
    145.  
    146. if (m_title.isEmpty()) {
    147. opt.currentText = combo->currentText();
    148. } else {
    149. opt.currentText = m_title.arg(combo->count());
    150. }
    151. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    152. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
    153.  
    154. return true;
    155. }
    156. return QStyledItemDelegate::eventFilter(object, event);
    157. }
    158.  
    159. void ComboUpDownDelegate::setTitle(const QString& title) {
    160. m_title = title;
    161. }
    162.  
    163. QString ComboUpDownDelegate::title() const {
    164. return m_title;
    165. }
    To copy to clipboard, switch view to plain text mode 

    And my code where I change the data of the combo:
    Qt Code:
    1. comboDevices->setItemData(index, 0, ComboUpDownDelegate::ICON2_ROLE);
    2. // Show the running icon
    3. comboDevices->setItemData(index, 3, ComboUpDownDelegate::ICON3_ROLE);
    4. comboDevices->update();
    To copy to clipboard, switch view to plain text mode 

    This code when the popup of the combobox is opened, the icons don't refresh

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

    Default Re: Update the combobox delegate in runtime while the popup is opened

    I solved the problem.

    The thread that changed the value of each role was not the main thread. To solve it I've emitted a queued signal and without changing any line of code, the combo items refresh with the opened popup. I hope my solution will be useful for other developers.

    Thanks

Similar Threads

  1. Replies: 0
    Last Post: 18th August 2013, 18:07
  2. tooltip popup in delegate
    By iwatsu in forum Newbie
    Replies: 4
    Last Post: 1st March 2012, 01:40
  3. set combobox delegate on some view items
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 8th March 2011, 14:36
  4. How to get index of a combobox delegate selection
    By vieraci in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2009, 16:37
  5. Combobox Delegate 25.000 records
    By aekilic in forum Qt Programming
    Replies: 9
    Last Post: 29th July 2008, 12:26

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.