I added QVariant to the code but the table still displays boolean as ture or false.
{
...
case 4:
return QVariant(Registers
[row
].
wrProtect);
// bool break;
...
case Qt::CheckStateRole:
if(col == 4)
{
if(Registers[row].wrProtect == true)
{
}
else
{
}
}
...
}
QVariant RegisterModel::data(const QModelIndex &index, int role) const
{
...
case 4:
return QVariant(Registers[row].wrProtect); // bool
break;
...
case Qt::CheckStateRole:
if(col == 4)
{
if(Registers[row].wrProtect == true)
{
return QVariant(Qt::Checked);
}
else
{
return QVariant(Qt::Unchecked);
}
}
...
}
To copy to clipboard, switch view to plain text mode
I want to use delegate. When I double-click the cell a new checkbox appers on the left in the cell. Checkbox is not displayed when I don't click.
checkboxdelegate.h
#ifndef CHECKBOXDELEGATE_H
#define CHECKBOXDELEGATE_H
#include <QStyledItemDelegate>
#include <QTableView>
#include <QObject>
#include <QDebug>
#include <QCheckBox>
#define DEB qDebug().noquote() <<
class CheckBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit CheckBoxDelegate
(QObject *parent
= 0);
void updateEditorGeometry
(QWidget *editor,
private slots:
void setData(bool value);
};
#endif // CHECKBOXDELEGATE_H
#ifndef CHECKBOXDELEGATE_H
#define CHECKBOXDELEGATE_H
#include <QStyledItemDelegate>
#include <QTableView>
#include <QObject>
#include <QDebug>
#include <QCheckBox>
#define DEB qDebug().noquote() <<
class CheckBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit CheckBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
mutable QCheckBox *checkBox;
private slots:
void setData(bool value);
};
#endif // CHECKBOXDELEGATE_H
To copy to clipboard, switch view to plain text mode
checkboxdelegate.cpp
#include "checkboxdelegate.h"
#include <QComboBox>
#include <QCheckBox>
#include <QRect>
#include <QPainter>
#include <QSpinBox>
#include <QApplication>
#include <QSignalMapper>
#include "booleanwidget.h"
CheckBoxDelegate
::CheckBoxDelegate(QObject *parent
) : QStyledItemDelegate(parent)
{
}
{
QObject::connect(checkBox,
SIGNAL(toggled
(bool)),
this,
SLOT(setData
(bool)));
return checkBox;
}
{
if(QCheckBox *cb
= static_cast<QCheckBox
*>
(editor
)) {
bool checked = index.model()->data(index, Qt::DisplayRole /* Qt::EditRole */).toBool();
DEB "Checked: " << checked;
cb->setChecked(checked);
}
}
{
if(QCheckBox *cb
= static_cast<QCheckBox
*>
(editor
)) // save the current text of the combo box as the current value of the item
model->setData(index, cb->isChecked());
else
QStyledItemDelegate::setModelData(editor, model, index);
}
void CheckBoxDelegate
::updateEditorGeometry(QWidget *editor,
editor->setGeometry(option.rect);
}
}
void CheckBoxDelegate::setData(bool value)
{
emit commitData(checkBox);
}
#include "checkboxdelegate.h"
#include <QComboBox>
#include <QCheckBox>
#include <QRect>
#include <QPainter>
#include <QSpinBox>
#include <QApplication>
#include <QSignalMapper>
#include "booleanwidget.h"
CheckBoxDelegate::CheckBoxDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
QWidget* CheckBoxDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
checkBox = new QCheckBox(parent);
QObject::connect(checkBox, SIGNAL(toggled(bool)), this, SLOT(setData(bool)));
return checkBox;
}
void CheckBoxDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const
{
if(QCheckBox *cb = static_cast<QCheckBox *>(editor)) {
bool checked = index.model()->data(index, Qt::DisplayRole /* Qt::EditRole */).toBool();
DEB "Checked: " << checked;
cb->setChecked(checked);
}
}
void CheckBoxDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
if(QCheckBox *cb = static_cast<QCheckBox *>(editor))
// save the current text of the combo box as the current value of the item
model->setData(index, cb->isChecked());
else
QStyledItemDelegate::setModelData(editor, model, index);
}
void CheckBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
editor->setGeometry(option.rect);
}
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
}
void CheckBoxDelegate::setData(bool value)
{
emit commitData(checkBox);
}
To copy to clipboard, switch view to plain text mode
What am I missing?
Bookmarks