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
// connect edit delegates
CheckListDelegate* daysdelegate = new CheckListDelegate(this);
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
#include <QtWidgets/QWidget>
#include <QtCore/QStringList>
#include <QtWidgets/QComboBox>
{
Q_OBJECT
public:
~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:
protected:
QVector<QStandardItem*> Items;
};
#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;
};
To copy to clipboard, switch view to plain text mode
checkstatecombobox.cpp
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>
#include "checkstatecombobox.hpp"
{
setModel(model);
for ( int i = 0; i < l_items.count() ; ++i )
{
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);
}
}
CheckStateComboBox::~CheckStateComboBox()
{
delete model;
}
void CheckStateComboBox
::addItem(QString &text, Qt
::CheckState checkState
) {
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)
{
{
addItem(itemData, checkState);
}
}
{
Q_UNUSED(i)
//qDebug() << "Item checked.....";
}
#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.....";
}
To copy to clipboard, switch view to plain text mode
checklistdelegate.hpp
#include <QtCore/QObject>
#include <QtWidgets/QWidget>
#include <QtGui/QStandardItem>
#include <QtWidgets/QStyledItemDelegate>
#include <QtWidgets/QStyleOptionViewItem>
class CheckStateComboBox;
class CheckListDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CheckListDelegate
(QWidget *parent
=0);
~CheckListDelegate();
private:
};
#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;
};
To copy to clipboard, switch view to plain text mode
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() { }
{
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;
}
{
Q_UNUSED(i)
QRect l_rect
= option.
rect;
l_rect.adjust(0,0,0,l_rect.height()*0.3);
editor->setGeometry(l_rect);
}
{
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);
}
}
{
quint16 i = 0;
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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks