PDA

View Full Version : QCheckBox Item In QDropdown



ad5xj
1st November 2015, 14:12
I have a need for a combobox that has a list of items that are check-able.

I found similar code on another site but it has not provided a complete solution.

I have constructed a working edit ComboBox delegate and the delegate appears as expected listing the items. However, I am unable to set or unset the check attribute by clicking on the item ... or any other keyboard or mouse action for that matter.

The one difference in my delegate and the other examples is that I am using QVector<QStandardItems*> items - instead of std::vector.

I have noticed that if I add the selection attribute, I can select the item and the dropbox closes and the desired result occurs for that item only. That is not the action I desire. I wish to check all appropriate items on the list then close the dropdown and evaluate the list.

Here is my unfinished code for discussion. I know there are better ways to do this but I have to start somewhere.

The code here does present a combobox in the appropriate column / cell of a table view with the items listed properly. However, the items do not present as QCheckbox items -- merely text item. The also do not allow a selection of any kind. I have missed something somewhere and I am not sure what.

I have looked at the other posts on similar issues but it has not revealed anything obvious to me.

editdaysdelegate.hpp


#include <QtCore/QSize>
#include <QtCore/QModelIndex>
#include <QtGui/QStandardItemModel>
#include <QtWidgets/QApplication>
#include <QtWidgets/QStylePainter>
#include <QtWidgets/QWidget>
#include <QtWidgets/QAbstractItemView>
#include <QtWidgets/QStyleOptionViewItem>
#include <QtWidgets/QItemDelegate>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QCheckBox>

#define Sun_On 1;
#define Mon_On 2;
#define Tue_On 4;
#define Wed_On 8;
#define Thu_On 16;
#define Fri_On 32;
#define Sat_On 64;

#define Sun_Off 63;
#define Mon_Off 95;
#define Tue_Off 111;
#define Wed_Off 119;
#define Thu_Off 123;
#define Fri_Off 125;
#define Sat_Off 126;

class CheckBoxListDelegate : public QItemDelegate
{
Q_OBJECT

public:
CheckBoxListDelegate(QObject *parent);
virtual ~CheckBoxListDelegate();

QWidget *createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index
) const Q_DECL_OVERRIDE;

void setEditorData(QWidget *editor,const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;
void setCurrentValue(const quint8 val);
private slots:
void slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight);

private:
quint8 m_value; // the current bit matrix value from the table

QVector<QStandardItem *> items;

QComboBox *combo;

QStandardItemModel* model;

QStandardItem* item1;
QStandardItem* item2;
QStandardItem* item3;
QStandardItem* item4;
QStandardItem* item5;
QStandardItem* item6;
QStandardItem* item7;
};


editdaysdelegate.cpp


#include <QtCore/QDebug>
#include <QtCore/QVector>
#include <QtWidgets/QStyle>
#include <QtWidgets/QStyleOption>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>
#include <QtGui/QPainter>

#include "editdaysdelegate.hpp"

//==== Delegate implementation ====//
CheckBoxListDelegate::CheckBoxListDelegate(QObject *parent) : QItemDelegate(parent)
{
m_value = 0;

combo = new QComboBox; // the widget to be delegated

model = new QStandardItemModel; // model for the delegate

item1 = new QStandardItem;
item1->setText("Sun");
item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item1->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(0, item1);
items.append(item1);

item2 = new QStandardItem;
item2->setText("Mon");
item2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item2->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(1, item2);
items.append(item2);

item3 = new QStandardItem;
item3->setText("Tue");
item3->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item3->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(2, item3);
items.append(item3);

item4 = new QStandardItem;
item4->setText("Wed");
item4->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item4->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(3, item4);
items.append(item4);

item5 = new QStandardItem;
item5->setText("Thu");
item5->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item5->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(4, item5);
items.append(item5);

item6 = new QStandardItem;
item6->setText("Fri");
item6->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item6->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(5, item6);
items.append(item6);

item7 = new QStandardItem;
item7->setText("Sat");
item7->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item7->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(6, item7);
items.append(item7);

connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this, SLOT(slot_changed(const QModelIndex &, const QModelIndex &)));

combo->setModel(model);
}

CheckBoxListDelegate::~CheckBoxListDelegate()
{
delete item1;
delete item2;
delete item3;
delete item4;
delete item5;
delete item6;
delete item7;
delete model;
}

void CheckBoxListDelegate::setCurrentValue(const quint8 val)
{
m_value = val;
}

void CheckBoxListDelegate::slot_changed(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
Q_UNUSED(bottomRight)
quint16 row;

row = topLeft.row();
QStandardItem *item = items[row];
if ( item->checkState() == Qt::Unchecked )
{
switch ( item->row() )
{
case 0:
m_value = m_value & Sun_Off;
break;
case 1:
m_value = m_value & Mon_Off;
break;
case 2:
m_value = m_value & Tue_Off;
break;
case 3:
m_value = m_value & Wed_Off;
break;
case 4:
m_value = m_value & Thu_Off;
break;
case 5:
m_value = m_value & Fri_Off;
break;
case 6:
m_value = m_value & Sat_Off;
break;
}
}
else if ( item->checkState() == Qt::Checked )
{
switch ( item->row() )
{
case 0:
m_value = m_value |= Sun_On;
break;
case 1:
m_value = m_value |= Mon_On;
break;
case 2:
m_value = m_value |= Tue_On;
break;
case 3:
m_value = m_value |= Wed_On;
break;
case 4:
m_value = m_value |= Thu_On;
break;
case 5:
m_value = m_value |= Fri_On;
break;
case 6:
m_value = m_value |= Sat_On;
break;
}
}
}

QWidget* CheckBoxListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option)
Q_UNUSED(index)

// create check box as our editor
QComboBox *editor = new QComboBox(parent);
editor->setModel(model);
return editor;
}

void CheckBoxListDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
//set editor data
QComboBox myEditor = static_cast<QComboBox>(editor);
myEditor->itemDelegate()->setModelData(editor,model,index);
}

void CheckBoxListDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const
{
//get the value from the editor (CheckBox)
QCheckBox myEditor = static_cast<QCheckBox>(editor);
bool value = myEditor->isChecked();

//set model data
QMap<int,QVariant> data;
data.insert(Qt::DisplayRole,myEditor->text());
data.insert(Qt::UserRole,value);
model->setItemData(index,data);
}

prasad_N
3rd November 2015, 07:48
I Implemented much complex one long back , I am pasting simplified version of that.
Sorry If i miss some thing in the code because I copy paste my old code here.


Below snippet is for : will show set of checkable items in Combo box & only checked items data will be shown in editor with ; separated


//Checkable combo box

class checkStateComboBox : public QComboBox
{
Q_OBJECT

protected:
QStandardItemModel* Model;
QVector<QStandardItem*> Items;

public:
checkStateComboBox( QStringList l_items = QStringList(), QWidget * parent = 0 ) : QComboBox(parent)
{
Model = new QStandardItemModel;
setModel(Model);

for(int i=0; i<l_items.count() ;i++)
{
QStandardItem* Item = new QStandardItem;
Item->setText(l_items.at(i));
Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
Item->setData(Qt::Checked, Qt::CheckStateRole);
Model->insertRow(Model->rowCount(), Item);
Items.push_back(Item);
}
connect(Model, SIGNAL( itemChanged(QStandardItem*) ), this, SLOT(itemChanged(QStandardItem*) ) );
}
~checkStateComboBox(){}

void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked)
{
QStandardItem* Item = new QStandardItem;
Item->setText(text);
Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
Item->setData(checkState, Qt::CheckStateRole);
Model->insertRow(Model->rowCount(), Item);
Items.push_back(Item);
}

void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked)
{
foreach (QString itemData, list)
{
addItem(itemData, checkState);
}
}

const QVector<QStandardItem*>& getItems() const
{
return Items;
}

const QStandardItem* getItem(const int& index) const
{
if(Items.size() <= index){
return NULL;
}

return Items.at(index);
}

public slots:
void itemChanged(QStandardItem* /*item*/)
{
//qDebug() << "Item checked.....";
}
};




//Delegate implementation

propertyItemDelegate::propertyItemDelegate(QObject *parent): QStyledItemDelegate(parent)
{
}

QWidget *propertyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex & index ) const
{
QWidget* l_editor = new checkStateComboBox();
l_editor->setParent(parent);

return l_editor;
}

void propertyItemDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
{
QStringList l_itemsData = //get the list you want to show;
QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor


//this logic is to check items which are previously checked based on data I am showing in editor
//I get the data which I am showing in editor & I know those are the only items I checked
checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(f_widget);
foreach(QString l_itemData, l_itemsData)
{
bool l_checkState = l_modelData.contains(l_itemData);
Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
l_multiValuedWidget->addItem(l_itemData, l_state);
}
}

void propertyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QString l_data = QString::null;

checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(f_widget);
int size = l_multiValuedWidget->getItems().count();

for(int i=0; i < size; i++)
{
const QStandardItem* l_item = l_multiValuedWidget->getItem(i);
//data of checked items only
if(l_item && (Qt::Checked == l_item->checkState()))
{
QString l_itemData = l_item->data(Qt::DisplayRole).toString();
((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
}
}

model->setData(index, l_data, Qt::EditRole);
}

void propertyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/ ) const
{
QRect l_rect = option.rect;
l_rect.adjust(0,0,0,l_rect.height()*0.3);
editor->setGeometry(l_rect);
}

ad5xj
3rd November 2015, 19:02
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

prasad_N
4th November 2015, 10:08
Can I see some code please ?




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) ?

ad5xj
4th November 2015, 15:07
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:


// connect edit delegates
CheckListDelegate* daysdelegate = new CheckListDelegate(this);
ui->tblView->setItemDelegateForColumn(6, daysdelegate); // days


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


checkstatecombobox.hpp


#include <QtWidgets/QWidget>
#include <QtCore/QStringList>
#include <QtWidgets/QComboBox>

class QStandardItem;
class QStandardItemModel;

class CheckStateComboBox : public QComboBox
{
Q_OBJECT

public:
CheckStateComboBox(QStringList l_items, QWidget *parent=0);
~CheckStateComboBox();

void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked);
void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked);

const QVector<QStandardItem*>& getItems() const { return Items; }
const QStandardItem* getItem(const int& index) const { if (Items.size() <= index) return NULL; return Items.at(index); }

public slots:
void itemChanged(QStandardItem*);

protected:
QStandardItemModel* model;
QVector<QStandardItem*> Items;
};



checkstatecombobox.cpp


#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>

#include "checkstatecombobox.hpp"

CheckStateComboBox::CheckStateComboBox(QStringList l_items, QWidget *parent) : QComboBox(parent)
{
model = new QStandardItemModel;
setModel(model);

for ( int i = 0; i < l_items.count() ; ++i )
{
QStandardItem* Item = new QStandardItem;
Item->setText(l_items.at(i));
Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
Item->setData(Qt::Checked, Qt::CheckStateRole);
model->insertRow(model->rowCount(), Item);
Items.push_back(Item);
}
connect(model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(itemChanged(QStandardItem*)));
}

CheckStateComboBox::~CheckStateComboBox()
{
delete model;
}

void CheckStateComboBox::addItem(QString &text, Qt::CheckState checkState)
{
QStandardItem* Item = new QStandardItem;
Item->setText(text);
Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
Item->setData(checkState, Qt::CheckStateRole);
model->insertRow(model->rowCount(), Item);
Items.push_back(Item);
}

void CheckStateComboBox::addItems(QStringList& list, Qt::CheckState checkState)
{
foreach (QString itemData, list)
{
addItem(itemData, checkState);
}
}

void CheckStateComboBox::itemChanged(QStandardItem* i)
{
Q_UNUSED(i)
//qDebug() << "Item checked.....";
}



checklistdelegate.hpp


#include <QtCore/QObject>
#include <QtWidgets/QWidget>
#include <QtGui/QStandardItem>
#include <QtWidgets/QStyledItemDelegate>
#include <QtWidgets/QStyleOptionViewItem>

class QModelIndex;

class CheckStateComboBox;

class CheckListDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
CheckListDelegate(QWidget *parent=0);
~CheckListDelegate();


void setEditorData(QWidget *f_widget, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const;
QWidget createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

private:
QStringList l_itemsData;
QWidget *l_editor;
};


checklistdelegate.cpp


#include <QtCore/QModelIndex>
#include <QtWidgets/QWidget>
#include <QtWidgets/QStyledItemDelegate>

#include "checkstatecombobox.hpp"
#include "checklistdelegate.hpp"

CheckListDelegate::CheckListDelegate(QWidget *parent) : QStyledItemDelegate(parent)
{
//
}
CheckListDelegate::~CheckListDelegate() { }

QWidget CheckListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
Q_UNUSED(parent)
Q_UNUSED(option)
Q_UNUSED(index)

l_itemsData << "Sun" << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat";
l_editor = new CheckStateComboBox(l_itemsData,parent);
return l_editor;
}

void CheckListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &i) const
{
Q_UNUSED(i)
QRect l_rect = option.rect;
l_rect.adjust(0,0,0,l_rect.height()*0.3);
editor->setGeometry(l_rect);
}

void CheckListDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
{
QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor

//this logic is to check items which are previously checked based on data I am showing in editor
//I get the data which I am showing in editor & I know those are the only items I checked
CheckStateComboBox* l_multiValuedWidget = static_cast<CheckStateComboBox*>(f_widget);
foreach ( QString l_itemData, l_itemsData )
{
bool l_checkState = l_modelData.contains(l_itemData);
Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
l_multiValuedWidget->addItem(l_itemData, l_state);
}
}

void CheckListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
quint16 i = 0;
QString l_data = QString::null;
const QStandardItem* l_item;

CheckStateComboBox* l_multiValuedWidget = static_cast<CheckStateComboBox*>(editor);
int size = l_multiValuedWidget->getItems().count();

for ( i = 0; i < size; ++i )
{
l_item = l_multiValuedWidget->getItem(i);
//data of checked items only
if ( l_item && (Qt::Checked == l_item->checkState()) )
{
QString l_itemData = l_item->data(Qt::DisplayRole).toString();
((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
}
}

model->setData(index, l_data, Qt::EditRole);
}

prasad_N
4th November 2015, 17:01
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


#include <QApplication>
#include <QHeaderView>
#include <QStandardItemModel>
#include <QTableView>
#include "delegate.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QStandardItemModel model(4, 2);
QTableView tableView;
tableView.setModel(&model);

propertyItemDelegate delegate;
tableView.setItemDelegate(&delegate);

tableView.horizontalHeader()->setStretchLastSection(true);

for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row + 1) * (column + 1)));
}

tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
tableView.show();
return app.exec();
}



//delegate.h

#ifndef DELEGATE_H
#define DELEGATE_H

#include <QComboBox>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QVector>
#include <QStringList>


class checkStateComboBox : public QComboBox
{
Q_OBJECT

protected:
QStandardItemModel* Model;
QVector<QStandardItem*> Items;

public:
checkStateComboBox( QStringList l_items = QStringList(), QWidget * parent = 0 ) : QComboBox(parent)
{
Model = new QStandardItemModel;
setModel(Model);

for(int i=0; i<l_items.count() ;i++)
{
QStandardItem* Item = new QStandardItem;
Item->setText(l_items.at(i));
Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
Item->setData(Qt::Checked, Qt::CheckStateRole);
Model->insertRow(Model->rowCount(), Item);
Items.push_back(Item);
}
connect(Model, SIGNAL( itemChanged(QStandardItem*) ), this, SLOT(itemChanged(QStandardItem*) ) );
}
~checkStateComboBox(){}

void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked)
{
QStandardItem* Item = new QStandardItem;
Item->setText(text);
Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
Item->setData(checkState, Qt::CheckStateRole);
Model->insertRow(Model->rowCount(), Item);
Items.push_back(Item);
}

void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked)
{
foreach (QString itemData, list)
{
addItem(itemData, checkState);
}
}

const QVector<QStandardItem*>& getItems() const
{
return Items;
}

const QStandardItem* getItem(const int& index) const
{
if(Items.size() <= index){
return NULL;
}

return Items.at(index);
}

public slots:
void itemChanged(QStandardItem* /*item*/)
{
//qDebug() << "Item checked.....";
}
};


//Delegate class
class propertyItemDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
propertyItemDelegate(QObject *parent=0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;

void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const Q_DECL_OVERRIDE;

void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;


QStringList m_testList;
};

#endif




//delegate.cpp


#include "delegate.h"

propertyItemDelegate::propertyItemDelegate(QObject *parent): QStyledItemDelegate(parent)
{
m_testList << "Sunday" << "Monday" << "tuesday" << "wednesday" << "friday" << "satday";
}

QWidget *propertyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex & index ) const
{
QWidget* l_editor = new checkStateComboBox();
l_editor->setParent(parent);

return l_editor;
}

void propertyItemDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
{
QStringList l_itemsData = m_testList; //get the list you want to show;
QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor


//this logic is to check items which are previously checked based on data I am showing in editor
//I get the data which I am showing in editor & I know those are the only items I checked
checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(f_widget);
foreach(QString l_itemData, l_itemsData)
{
bool l_checkState = l_modelData.contains(l_itemData);
Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
l_multiValuedWidget->addItem(l_itemData, l_state);
}
}

void propertyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QString l_data = QString::null;

checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(editor);
int size = l_multiValuedWidget->getItems().count();

for(int i=0; i < size; i++)
{
const QStandardItem* l_item = l_multiValuedWidget->getItem(i);
//data of checked items only
if(l_item && (Qt::Checked == l_item->checkState()))
{
QString l_itemData = l_item->data(Qt::DisplayRole).toString();
((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
}
}

model->setData(index, l_data, Qt::EditRole);
}

void propertyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/ ) const
{
QRect l_rect = option.rect;
l_rect.adjust(0,0,0,l_rect.height()*0.3);
editor->setGeometry(l_rect);
}

ad5xj
4th November 2015, 18:35
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.

prasad_N
4th November 2015, 18:50
sorry, not sure what is the issue. you can check my attachment of result for the same code.


11499

ad5xj
4th November 2015, 19:13
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.

prasad_N
5th November 2015, 08:21
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).

ad5xj
5th November 2015, 15:25
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.