Results 1 to 7 of 7

Thread: QCheckBox as item of QComboBox

  1. #1
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default QCheckBox as item of QComboBox

    Hello.
    I bumped into a problem of adding an item QCheckBox into QComboBox. What I`ve found is the code for creating ItemDelegate for QComboBox, but it makes all of items in QComboBox as QCheckBox. My task is to add just one check box into the list of common combobox`s items...
    Here is a code I found:
    Qt Code:
    1. class CheckBoxList: public QComboBox
    2. {
    3. Q_OBJECT;
    4.  
    5. public:
    6. CheckBoxList(QWidget *widget = 0);
    7. virtual ~CheckBoxList();
    8. virtual void paintEvent(QPaintEvent *);
    9. void SetDisplayText(QString text);
    10. QString GetDisplayText() const;
    11.  
    12. private:
    13. QString m_DisplayText;
    14. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class CheckBoxListDelegate : public QItemDelegate
    2. {
    3. public:
    4. CheckBoxListDelegate(QObject *parent)
    5. : QItemDelegate(parent)
    6. {
    7. ;
    8. }
    9.  
    10. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    11. const QModelIndex &index) const
    12. {
    13. //Get item data
    14. bool value = index.data(Qt::UserRole).toBool();
    15. QString text = index.data(Qt::DisplayRole).toString();
    16.  
    17. // fill style options with item data
    18. const QStyle *style = QApplication::style();
    19. opt.state |= value ? QStyle::State_On : QStyle::State_Off;
    20. opt.state |= QStyle::State_Enabled;
    21. opt.text = text;
    22. opt.rect = option.rect;
    23.  
    24. // draw item data as CheckBox
    25. style->drawControl(QStyle::CE_CheckBox,&opt,painter);
    26. }
    27.  
    28. QWidget *createEditor(QWidget *parent,
    29. const QStyleOptionViewItem & option ,
    30. const QModelIndex & index ) const
    31. {
    32. // create check box as our editor
    33. QCheckBox *editor = new QCheckBox(parent);
    34. return editor;
    35. }
    36.  
    37. void setEditorData(QWidget *editor,
    38. const QModelIndex &index) const
    39. {
    40. //set editor data
    41. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
    42. myEditor->setText(index.data(Qt::DisplayRole).toString());
    43. myEditor->setChecked(index.data(Qt::UserRole).toBool());
    44. }
    45.  
    46. void setModelData(QWidget *editor, QAbstractItemModel *model,
    47. const QModelIndex &index) const
    48. {
    49. //get the value from the editor (CheckBox)
    50. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
    51. bool value = myEditor->isChecked();
    52.  
    53. //set model data
    54. QMap<int,QVariant> data;
    55. data.insert(Qt::DisplayRole,myEditor->text());
    56. data.insert(Qt::UserRole,value);
    57. model->setItemData(index,data);
    58. }
    59. };
    60.  
    61.  
    62.  
    63. CheckBoxList::CheckBoxList(QWidget *widget )
    64. :QComboBox(widget),m_DisplayText("")
    65. {
    66. // set delegate items view
    67. view()->setItemDelegate(new CheckBoxListDelegate(this));
    68.  
    69. // Enable editing on items view
    70. view()->setEditTriggers(QAbstractItemView::CurrentChanged);
    71. }
    72.  
    73.  
    74. CheckBoxList::~CheckBoxList()
    75. {
    76. ;
    77. }
    78.  
    79.  
    80. void CheckBoxList::paintEvent(QPaintEvent *)
    81. {
    82. QStylePainter painter(this);
    83. painter.setPen(palette().color(QPalette::Text));
    84.  
    85. // draw the combobox frame, focusrect and selected etc.
    86. initStyleOption(&opt);
    87.  
    88. // if no display text been set , use "..." as default
    89. if(m_DisplayText.isNull())
    90. opt.currentText = "...";
    91. else
    92. opt.currentText = m_DisplayText;
    93. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    94.  
    95. // draw the icon and text
    96. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
    97. }
    98.  
    99.  
    100. void CheckBoxList::SetDisplayText(QString text)
    101. {
    102. m_DisplayText = text;
    103. }
    104.  
    105. QString CheckBoxList::GetDisplayText() const
    106. {
    107. return m_DisplayText;
    108. }
    To copy to clipboard, switch view to plain text mode 

    Is anyone have thoughts how to improve this code to get possibility for adding into QComboBox 2 types of items: QCheckBox and common QComboBox items?
    Or there is some other ways to achieve such behaviour?

  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: QCheckBox as item of QComboBox

    Should be simple if you use QModelIndex to track items

    For example if you want only item at index 3 to be QCheckBox, and rest to be standard QComboBox items, then you need to modify this way


    Qt Code:
    1. QWidget *createEditor(QWidget *parent,
    2. const QStyleOptionViewItem & option ,
    3. const QModelIndex & index ) const
    4. {
    5. if(index.row() == 2) // Check for row 3 //Add this
    6. {
    7. // create check box as our editor
    8. QCheckBox *editor = new QCheckBox(parent);
    9. return editor;
    10. }
    11. return 0; //Add this, for default
    12. }
    To copy to clipboard, switch view to plain text mode 

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

    Grinchman (23rd June 2011)

  4. #3
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QCheckBox as item of QComboBox

    Thanks! It works! But next questions is how to check that index.row() is the last row in combo box?

  5. #4
    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: QCheckBox as item of QComboBox

    Qt Code:
    1. if(index.row() == index.model()->rowCount() - 1)
    2. //then last row
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QCheckBox as item of QComboBox

    Sorry for my inattention in reading documentation. I`ve did it already the same way )
    Let this topic be opened. Coz I just studying that part of Qt (QModelIndex, QItemDelegate ant etc.) and I`m sure that new questions would appear.

    The next thing I would try to do is to get (and set too) data from(to) checkbox.

  7. #6
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QCheckBox as item of QComboBox

    I`ve done it.
    I just added ne set/get functions:
    Qt Code:
    1. class CheckBoxList: public QComboBox
    2. {
    3. Q_OBJECT;
    4.  
    5. public:
    6. CheckBoxList(QWidget *widget = 0);
    7. virtual ~CheckBoxList();
    8. bool GetAutoCalcValue();
    9. void SetAutoCalcValue(bool checked);
    10. };
    To copy to clipboard, switch view to plain text mode 

    And the implentation:
    Qt Code:
    1. bool CheckBoxList::GetAutoCalcValue()
    2. {
    3. QModelIndex index = model()->index(model()->rowCount() - 1, 0);
    4. return index.data(Qt::UserRole).toBool();
    5. }
    6.  
    7. void CheckBoxList::SetAutoCalcValue(bool checked)
    8. {
    9. QModelIndex index = model()->index(model()->rowCount() - 1, 0);
    10.  
    11. QString text = index.data(Qt::DisplayRole).toString();
    12.  
    13. QMap<int,QVariant> data;
    14. data.insert(Qt::DisplayRole,text);
    15. data.insert(Qt::UserRole,checked);
    16. model()->setItemData(index,data);
    17. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QCheckBox as item of QComboBox

    Wouldn't it be easier to just use a model with the combobox and return the ItemIsUserCheckable flag for the items you want checkable?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 10th March 2011, 11:44
  2. Replies: 2
    Last Post: 30th March 2010, 22:51
  3. Qcombobox item rect?
    By shud in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2009, 12:07
  4. QcomboBox item height
    By Beppe in forum Qt Programming
    Replies: 9
    Last Post: 16th June 2009, 11:31
  5. QComboBox and item
    By drow in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2006, 09:04

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.