Results 1 to 11 of 11

Thread: QCheckBox Item In QDropdown

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: QCheckBox Item In QDropdown

    prasad_N

    Thanks for your reply.

    Well your code does little different than mine. After creating it for use in my project I got a checkbox at the top of the form instead of the column / cell of the displayed table view being edited and the items are displayed twice and do not appear as checkboxes -- just text items that are not selectable -- no checkboxes at all.


    Maybe I did not indicate before, I am using Qt 5.5.1

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: QCheckBox Item In QDropdown

    Can I see some code please ?



    Quote Originally Posted by ad5xj View Post
    After creating it for use in my project I got a checkbox at the top of the form instead of the column / cell of the displayed table view being edited and the items are displayed twice and do not appear as checkboxes -- just text items that are not selectable -- no checkboxes at all.
    I got a checkbox at the top of the form - What do you mean form (view ?). ??
    the items are displayed twice and do not appear as checkboxes - If you see only check box then how you are able to see items (we can see items in combo box only) ?
    Last edited by prasad_N; 4th November 2015 at 12:00.
    Thanks :-)

  3. #3
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: QCheckBox Item In QDropdown

    Ok, Perhaps my reply was a bit vague.

    The QMainWindow is the form I referred to; although, I would also like to use this delegate in a QDialog as well. So I guess you could apply either term to the reference of "form" in my reply. I also re-formated your code to fit my project and programming style. I very well may have missed something in the interpretation.

    The resulting QComboBox does not contain any checkbox items - only text items -- none selectable -- nothing happens when I click on any item in the drop-down list. This has been the problem all along. With the original code I was able to display a combobox but none of the items were checkboxes nor selectable. The only difference I am finding is that your code is not placing the delegate in the column / cell selected for edit.

    I am using the delegate in a QTableView item in the UI of either form. It is assigned in the normal way using:

    In the QMainWindow or QDialog:
    Qt Code:
    1. // connect edit delegates
    2. CheckListDelegate* daysdelegate = new CheckListDelegate(this);
    3. ui->tblView->setItemDelegateForColumn(6, daysdelegate); // days
    To copy to clipboard, switch view to plain text mode 

    Here is the code as I have it now. Hope this helps.


    checkstatecombobox.hpp
    Qt Code:
    1. #include <QtWidgets/QWidget>
    2. #include <QtCore/QStringList>
    3. #include <QtWidgets/QComboBox>
    4.  
    5.  
    6. class CheckStateComboBox : public QComboBox
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. CheckStateComboBox(QStringList l_items, QWidget *parent=0);
    12. ~CheckStateComboBox();
    13.  
    14. void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked);
    15. void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked);
    16.  
    17. const QVector<QStandardItem*>& getItems() const { return Items; }
    18. const QStandardItem* getItem(const int& index) const { if (Items.size() <= index) return NULL; return Items.at(index); }
    19.  
    20. public slots:
    21. void itemChanged(QStandardItem*);
    22.  
    23. protected:
    24. QVector<QStandardItem*> Items;
    25. };
    To copy to clipboard, switch view to plain text mode 

    checkstatecombobox.cpp
    Qt Code:
    1. #include <QtCore/QObject>
    2. #include <QtCore/QString>
    3. #include <QtGui/QStandardItem>
    4. #include <QtGui/QStandardItemModel>
    5.  
    6. #include "checkstatecombobox.hpp"
    7.  
    8. CheckStateComboBox::CheckStateComboBox(QStringList l_items, QWidget *parent) : QComboBox(parent)
    9. {
    10. model = new QStandardItemModel;
    11. setModel(model);
    12.  
    13. for ( int i = 0; i < l_items.count() ; ++i )
    14. {
    15. Item->setText(l_items.at(i));
    16. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    17. Item->setData(Qt::Checked, Qt::CheckStateRole);
    18. model->insertRow(model->rowCount(), Item);
    19. Items.push_back(Item);
    20. }
    21. connect(model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(itemChanged(QStandardItem*)));
    22. }
    23.  
    24. CheckStateComboBox::~CheckStateComboBox()
    25. {
    26. delete model;
    27. }
    28.  
    29. void CheckStateComboBox::addItem(QString &text, Qt::CheckState checkState)
    30. {
    31. Item->setText(text);
    32. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    33. Item->setData(checkState, Qt::CheckStateRole);
    34. model->insertRow(model->rowCount(), Item);
    35. Items.push_back(Item);
    36. }
    37.  
    38. void CheckStateComboBox::addItems(QStringList& list, Qt::CheckState checkState)
    39. {
    40. foreach (QString itemData, list)
    41. {
    42. addItem(itemData, checkState);
    43. }
    44. }
    45.  
    46. void CheckStateComboBox::itemChanged(QStandardItem* i)
    47. {
    48. Q_UNUSED(i)
    49. //qDebug() << "Item checked.....";
    50. }
    To copy to clipboard, switch view to plain text mode 


    checklistdelegate.hpp
    Qt Code:
    1. #include <QtCore/QObject>
    2. #include <QtWidgets/QWidget>
    3. #include <QtGui/QStandardItem>
    4. #include <QtWidgets/QStyledItemDelegate>
    5. #include <QtWidgets/QStyleOptionViewItem>
    6.  
    7.  
    8. class CheckStateComboBox;
    9.  
    10. class CheckListDelegate : public QStyledItemDelegate
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. CheckListDelegate(QWidget *parent=0);
    16. ~CheckListDelegate();
    17.  
    18.  
    19. void setEditorData(QWidget *f_widget, const QModelIndex &index) const;
    20. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    21. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const;
    22. QWidget createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    23.  
    24. private:
    25. QStringList l_itemsData;
    26. QWidget *l_editor;
    27. };
    To copy to clipboard, switch view to plain text mode 

    checklistdelegate.cpp
    Qt Code:
    1. #include <QtCore/QModelIndex>
    2. #include <QtWidgets/QWidget>
    3. #include <QtWidgets/QStyledItemDelegate>
    4.  
    5. #include "checkstatecombobox.hpp"
    6. #include "checklistdelegate.hpp"
    7.  
    8. CheckListDelegate::CheckListDelegate(QWidget *parent) : QStyledItemDelegate(parent)
    9. {
    10. //
    11. }
    12. CheckListDelegate::~CheckListDelegate() { }
    13.  
    14. QWidget CheckListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    15. {
    16. Q_UNUSED(parent)
    17. Q_UNUSED(option)
    18. Q_UNUSED(index)
    19.  
    20. l_itemsData << "Sun" << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat";
    21. l_editor = new CheckStateComboBox(l_itemsData,parent);
    22. return l_editor;
    23. }
    24.  
    25. void CheckListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &i) const
    26. {
    27. Q_UNUSED(i)
    28. QRect l_rect = option.rect;
    29. l_rect.adjust(0,0,0,l_rect.height()*0.3);
    30. editor->setGeometry(l_rect);
    31. }
    32.  
    33. void CheckListDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
    34. {
    35. QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor
    36.  
    37. //this logic is to check items which are previously checked based on data I am showing in editor
    38. //I get the data which I am showing in editor & I know those are the only items I checked
    39. CheckStateComboBox* l_multiValuedWidget = static_cast<CheckStateComboBox*>(f_widget);
    40. foreach ( QString l_itemData, l_itemsData )
    41. {
    42. bool l_checkState = l_modelData.contains(l_itemData);
    43. Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
    44. l_multiValuedWidget->addItem(l_itemData, l_state);
    45. }
    46. }
    47.  
    48. void CheckListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    49. {
    50. quint16 i = 0;
    51. QString l_data = QString::null;
    52. const QStandardItem* l_item;
    53.  
    54. CheckStateComboBox* l_multiValuedWidget = static_cast<CheckStateComboBox*>(editor);
    55. int size = l_multiValuedWidget->getItems().count();
    56.  
    57. for ( i = 0; i < size; ++i )
    58. {
    59. l_item = l_multiValuedWidget->getItem(i);
    60. //data of checked items only
    61. if ( l_item && (Qt::Checked == l_item->checkState()) )
    62. {
    63. QString l_itemData = l_item->data(Qt::DisplayRole).toString();
    64. ((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
    65. }
    66. }
    67.  
    68. model->setData(index, l_data, Qt::EditRole);
    69. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: QCheckBox Item In QDropdown

    I did quick experiment on Qt 5.4.0, I just edited spin box delegate example, It worked well. here is the code, you just use this as stand alone implementation. If it works fine then It should work in your project too.

    use this code as it is & let me know if you are still unable to see check boxes & can't check/uncheck them.


    //main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QHeaderView>
    3. #include <QStandardItemModel>
    4. #include <QTableView>
    5. #include "delegate.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QStandardItemModel model(4, 2);
    12. QTableView tableView;
    13. tableView.setModel(&model);
    14.  
    15. propertyItemDelegate delegate;
    16. tableView.setItemDelegate(&delegate);
    17.  
    18. tableView.horizontalHeader()->setStretchLastSection(true);
    19.  
    20. for (int row = 0; row < 4; ++row) {
    21. for (int column = 0; column < 2; ++column) {
    22. QModelIndex index = model.index(row, column, QModelIndex());
    23. model.setData(index, QVariant((row + 1) * (column + 1)));
    24. }
    25.  
    26. tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
    27. tableView.show();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 


    //delegate.h
    Qt Code:
    1. #ifndef DELEGATE_H
    2. #define DELEGATE_H
    3.  
    4. #include <QComboBox>
    5. #include <QStandardItemModel>
    6. #include <QStyledItemDelegate>
    7. #include <QVector>
    8. #include <QStringList>
    9.  
    10.  
    11. class checkStateComboBox : public QComboBox
    12. {
    13. Q_OBJECT
    14.  
    15. protected:
    16. QVector<QStandardItem*> Items;
    17.  
    18. public:
    19. checkStateComboBox( QStringList l_items = QStringList(), QWidget * parent = 0 ) : QComboBox(parent)
    20. {
    21. Model = new QStandardItemModel;
    22. setModel(Model);
    23.  
    24. for(int i=0; i<l_items.count() ;i++)
    25. {
    26. Item->setText(l_items.at(i));
    27. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    28. Item->setData(Qt::Checked, Qt::CheckStateRole);
    29. Model->insertRow(Model->rowCount(), Item);
    30. Items.push_back(Item);
    31. }
    32. connect(Model, SIGNAL( itemChanged(QStandardItem*) ), this, SLOT(itemChanged(QStandardItem*) ) );
    33. }
    34. ~checkStateComboBox(){}
    35.  
    36. void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked)
    37. {
    38. Item->setText(text);
    39. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    40. Item->setData(checkState, Qt::CheckStateRole);
    41. Model->insertRow(Model->rowCount(), Item);
    42. Items.push_back(Item);
    43. }
    44.  
    45. void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked)
    46. {
    47. foreach (QString itemData, list)
    48. {
    49. addItem(itemData, checkState);
    50. }
    51. }
    52.  
    53. const QVector<QStandardItem*>& getItems() const
    54. {
    55. return Items;
    56. }
    57.  
    58. const QStandardItem* getItem(const int& index) const
    59. {
    60. if(Items.size() <= index){
    61. return NULL;
    62. }
    63.  
    64. return Items.at(index);
    65. }
    66.  
    67. public slots:
    68. void itemChanged(QStandardItem* /*item*/)
    69. {
    70. //qDebug() << "Item checked.....";
    71. }
    72. };
    73.  
    74.  
    75. //Delegate class
    76. class propertyItemDelegate : public QStyledItemDelegate
    77. {
    78. Q_OBJECT
    79.  
    80. public:
    81. propertyItemDelegate(QObject *parent=0);
    82.  
    83. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    84. const QModelIndex &index) const Q_DECL_OVERRIDE;
    85.  
    86. void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    87. void setModelData(QWidget *editor, QAbstractItemModel *model,
    88. const QModelIndex &index) const Q_DECL_OVERRIDE;
    89.  
    90. void updateEditorGeometry(QWidget *editor,
    91. const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    92.  
    93.  
    94. QStringList m_testList;
    95. };
    96.  
    97. #endif
    To copy to clipboard, switch view to plain text mode 



    //delegate.cpp

    Qt Code:
    1. #include "delegate.h"
    2.  
    3. propertyItemDelegate::propertyItemDelegate(QObject *parent): QStyledItemDelegate(parent)
    4. {
    5. m_testList << "Sunday" << "Monday" << "tuesday" << "wednesday" << "friday" << "satday";
    6. }
    7.  
    8. QWidget *propertyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex & index ) const
    9. {
    10. QWidget* l_editor = new checkStateComboBox();
    11. l_editor->setParent(parent);
    12.  
    13. return l_editor;
    14. }
    15.  
    16. void propertyItemDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
    17. {
    18. QStringList l_itemsData = m_testList; //get the list you want to show;
    19. QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor
    20.  
    21.  
    22. //this logic is to check items which are previously checked based on data I am showing in editor
    23. //I get the data which I am showing in editor & I know those are the only items I checked
    24. checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(f_widget);
    25. foreach(QString l_itemData, l_itemsData)
    26. {
    27. bool l_checkState = l_modelData.contains(l_itemData);
    28. Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
    29. l_multiValuedWidget->addItem(l_itemData, l_state);
    30. }
    31. }
    32.  
    33. void propertyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    34. {
    35. QString l_data = QString::null;
    36.  
    37. checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(editor);
    38. int size = l_multiValuedWidget->getItems().count();
    39.  
    40. for(int i=0; i < size; i++)
    41. {
    42. const QStandardItem* l_item = l_multiValuedWidget->getItem(i);
    43. //data of checked items only
    44. if(l_item && (Qt::Checked == l_item->checkState()))
    45. {
    46. QString l_itemData = l_item->data(Qt::DisplayRole).toString();
    47. ((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
    48. }
    49. }
    50.  
    51. model->setData(index, l_data, Qt::EditRole);
    52. }
    53.  
    54. void propertyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/ ) const
    55. {
    56. QRect l_rect = option.rect;
    57. l_rect.adjust(0,0,0,l_rect.height()*0.3);
    58. editor->setGeometry(l_rect);
    59. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

  5. #5
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: QCheckBox Item In QDropdown

    I built a project exactly as you have it and the results are the same as my custom project.

    The QTableView allows the QComboBox to be displayed with all items added -- however, the items are text items NOT CHECKBOX items. No item can be selected from the list.

  6. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: QCheckBox Item In QDropdown

    sorry, not sure what is the issue. you can check my attachment of result for the same code.


    delegate.png
    Thanks :-)

  7. #7
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: QCheckBox Item In QDropdown

    I do not doubt you. All I am saying is that; I get the window and the table as shown. But, the items you show as check box items are not so for me.

    I am not sure if this is unique to my installation or a bug in Qt 5.5.1.

  8. #8
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: QCheckBox Item In QDropdown

    Even I am not sure, The quick check you can do is, Just try to build same code on another version (It worked for me on Qt4.8.6 & 5.4.0) It works fine with these versions then there is an issue with your version.
    One more thing you can try is use QItemDelegate instead of QStyledItemDelegate (Of course there is no much diff b/w them apart from QStyledItemDelegate uses current style to draw items, But it may get solved if there is a bug in QStyledItemDelegate).
    Thanks :-)

  9. #9
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: QCheckBox Item In QDropdown

    prasad_N

    Thanks for the replay. I did compile with QItemDelegate instead of the QStyledItemDelegate and no change in result.

    I am beginning to believe there is some kind of bug in the Item Delegate classes. I will try to run your test on Windows and see if it exists there as well. If so, I will post a bug report.


    UPDATE:

    I moved the code to my Windows XP SP3 platform and it runs as advertised. This tends to point to my Linux Mint installation. I am going to try a re-install of Qt 5.5 and see if that makes a difference. I have checked the dependencies and all are installed on my system so if the re-install produces the same result, I will have to post a bug report.
    Last edited by ad5xj; 5th November 2015 at 16:34.

Similar Threads

  1. QCheckBox
    By sajis997 in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2012, 15:15
  2. QCheckBox as item of QComboBox
    By Grinchman in forum Newbie
    Replies: 6
    Last Post: 23rd June 2011, 14:44
  3. About QCheckBox
    By liushuo0826 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th April 2010, 07:14
  4. QCheckBox value
    By Koas in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2009, 14:33
  5. QCheckbox
    By sonuani in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2008, 14:12

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
  •  
Qt is a trademark of The Qt Company.