Results 1 to 3 of 3

Thread: Delegate not calling createEditor()

  1. #1
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Delegate not calling createEditor()

    I have ComboBox with checkable items made with CheckBox delegate. When this ComboBox is opened the first item's delegate's createEditor() is not called but when you move to second item it is called and delegate is properly created. After this when you move back to first item then delegate is working. The problem is only with selecting first item for first time. If you have got only one item in comboBox this item is unselectable.

    This is my delegate's code:
    Qt Code:
    1. #include "checkboxlistdelegate.h"
    2.  
    3. CheckBoxListDelegate::CheckBoxListDelegate(QObject *parent)
    4. : QItemDelegate(parent) {
    5. }
    6.  
    7. void CheckBoxListDelegate::paint(QPainter *painter,
    8. const QStyleOptionViewItem &option,
    9. const QModelIndex &index) const {
    10. //Get item data
    11. bool value = index.data(Qt::UserRole).toBool();
    12. QString text = index.data(Qt::DisplayRole).toString();
    13. // fill style options with item data
    14. const QStyle *style = QApplication::style();
    15. opt.state |= value ? QStyle::State_On : QStyle::State_Off;
    16. opt.state |= QStyle::State_Enabled;
    17. opt.text = text;
    18. opt.rect = option.rect;
    19. opt.palette = QPalette(Qt::white);
    20. // draw item data as CheckBox
    21. style->drawControl(QStyle::CE_CheckBox,&opt,painter);
    22. }
    23.  
    24. QWidget* CheckBoxListDelegate::createEditor(QWidget *parent,
    25. const QStyleOptionViewItem& option,
    26. const QModelIndex & index ) const {
    27. QCheckBox *editor = new QCheckBox(parent);
    28. editor->setStyleSheet("QCheckBox {background-color: #aaaaaa; color: white;}");
    29. return editor;
    30. }
    31.  
    32. void CheckBoxListDelegate::setEditorData(QWidget *editor,
    33. const QModelIndex &index) const {
    34. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
    35. myEditor->setText(index.data(Qt::DisplayRole).toString());
    36. myEditor->setChecked(index.data(Qt::UserRole).toBool());
    37. }
    38.  
    39. void CheckBoxListDelegate::setModelData(QWidget *editor,
    40. const QModelIndex &index) const {
    41. //get the value from the editor (CheckBox)
    42. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
    43. bool value = myEditor->isChecked();
    44. //set model data
    45. QMap<int,QVariant> data;
    46. data.insert(Qt::DisplayRole,myEditor->text());
    47. data.insert(Qt::UserRole,value);
    48. model->setItemData(index,data);
    49. emit indexChanged(index);
    50. }
    51.  
    52. void CheckBoxListDelegate::updateEditorGeometry(QWidget *editor,
    53. const QStyleOptionViewItem &option,
    54. const QModelIndex &index ) const {
    55. Q_UNUSED(index);
    56. editor->setGeometry(option.rect);
    To copy to clipboard, switch view to plain text mode 

    And this is code of my QComboBox subclass:

    Qt Code:
    1. CheckBoxList::CheckBoxList(QWidget *widget)
    2. : QComboBox(widget),title(""),selection() {
    3. // set delegate items view
    4. view()->setItemDelegate(new CheckBoxListDelegate(this));
    5. view()->setStyleSheet("QAbstractItemView {background-color:white;}");
    6. // Enable editing on items view
    7. view()->setEditTriggers(QAbstractItemView::AllEditTriggers);
    8. connect(view()->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
    9. this, SLOT(onItemClicked(QModelIndex)));
    10. }
    11.  
    12. void CheckBoxList::paintEvent(QPaintEvent *) {
    13. QStylePainter painter(this);
    14. painter.setPen(palette().color(QPalette::Text));
    15. initStyleOption(&opt);
    16. opt.currentText = title;
    17. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    18. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
    19. }
    20.  
    21. void CheckBoxList::setDisplayText(QString text) {
    22. title = text;
    23. }
    24.  
    25. QString CheckBoxList::getDisplayText() const {
    26. return title;
    27. }
    28.  
    29. bool CheckBoxList::isChecked(const QModelIndex& index) const {
    30. return view()->model()->itemData(index).value(Qt::UserRole).toBool();
    31. }
    To copy to clipboard, switch view to plain text mode 

    Thank You!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Delegate not calling createEditor()

    First thing QAbstractItemView::AllEditTriggers is a strange thing, every action will start the editor, you will have problem selecting the item in the first place.

    When this ComboBox is opened the first item's delegate's createEditor() is not called but when you move to second item it is called and delegate is properly created.
    Try to set the current index to -1 when you start, and then try selecting the first item, and see the magic.

    The problem is only with selecting first item for first time. If you have got only one item in comboBox this item is unselectable.
    The problem is not with the first item, the problem is selecting (hovring) over an already selected item. Try this, set the index to 4 when you start, and move the mouse on to 4th item (note not to cross over any other items in the combo), you will need to take mouse out of the combo widget and bring it over the 4th item from outside directly.

    I think this is just the begining of other host of operational problems, and I think it is contributed by improper use of QAbstractItemView::AllEditTriggers. Also I don't think you are able to see the items normaly in the combo box, I mean only the drop down will work, but after selection the item will not be displayed in the combo.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    bmatous (18th April 2013)

  4. #3
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Delegate not calling createEditor()

    Thank you. Your advice was helpful the problem was really with currently selected index. So when I set currentIndex to -1 before every opening of ComboBox it is working properly.

    I have set AllEditTriggers because it was recommended on most of fora to do so to prevent future error. After you have advised me I have returned back to CurrentChanged which I have been using before.

Similar Threads

  1. return the editor pointer from createEditor
    By Naahmi in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2011, 09:32
  2. createEditor() is not called in custom delegate
    By landstreicher in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2010, 04:41
  3. Replies: 3
    Last Post: 5th April 2010, 22:20
  4. Replies: 1
    Last Post: 28th October 2009, 19:42
  5. QWidget* of createEditor
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2008, 18:48

Tags for this Thread

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.