#ifndef CBITEMDELEGATE_H
#define CBITEMDELEGATE_H
#include <QItemDelegate>
#include "checkbox.h"
#include <QTableView>
{
Q_OBJECT
public:
void setEditorData
(QWidget *editor,
void updateEditorGeometry
(QWidget *editor,
virtual bool editorEvent
(QEvent *event,
private:
private slots:
void slot_clicked(int row, Qt::CheckState cs);
signals:
void sig_clicked(int row, Qt::CheckState cs);
};
#endif //CBITEMDELEGATE_H
#ifndef CBITEMDELEGATE_H
#define CBITEMDELEGATE_H
#include <QItemDelegate>
#include "checkbox.h"
#include <QTableView>
class CBItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit CBItemDelegate(QTableView *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;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
virtual bool editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index);
private:
QTableView *my_table;
private slots:
void slot_clicked(int row, Qt::CheckState cs);
signals:
void sig_clicked(int row, Qt::CheckState cs);
};
#endif //CBITEMDELEGATE_H
To copy to clipboard, switch view to plain text mode
#include "cbitemdelegate.h"
#include <QApplication>
#include <QAbstractItemView>
#include <QtGui>
CBItemDelegate
::CBItemDelegate(QTableView *parent
){
my_table = parent;
}
CheckBox *editor = new CheckBox(index.row(), parent);
editor->installEventFilter(const_cast<CBItemDelegate*>(this));
editor->setFixedSize(24, 24);
editor->setFocusPolicy(Qt::StrongFocus);
connect(editor, SIGNAL(pressed(int,Qt::CheckState)),
this, SLOT(slot_clicked(int,Qt::CheckState)),
Qt::DirectConnection);
QRect rect
= my_table
->visualRect
(index
);
QPoint draw_at
(rect.
left() + (rect.
width() / 2) - (editor
->width
() / 2),
rect.top() + (rect.height() / 2) - (editor->height() / 2));
editor->move(draw_at);
return editor;
}
void CBItemDelegate::slot_clicked(int row, Qt::CheckState cs) {
emit sig_clicked(row, cs);
}
void CBItemDelegate
::setEditorData(QWidget *editor,
int value = index.model()->data(index, Qt::DisplayRole).toInt();
CheckBox *checkBox = static_cast<CheckBox*>(editor);
if(value == 1)
checkBox->setCheckState(Qt::Unchecked);
else
checkBox->setCheckState(Qt::Checked);
}
void CBItemDelegate
::setModelData(QWidget *editor,
CheckBox *checkBox = static_cast<CheckBox*>(editor);
int value;
if(checkBox->checkState() == Qt::Checked)
value = 1;
else
value = 0;
model->setData(index, value);
}
void CBItemDelegate
::updateEditorGeometry(QWidget *editor,
r.
setSize(QSize(24,
24));
r.moveTo((option.rect.width() / 2) - (editor->width() / 2),
(option.rect.height() / 2) - (editor->height() / 2));
editor->setGeometry(r);
QRect rect
= my_table
->visualRect
(index
);
QPoint draw_at
(rect.
left() + (rect.
width() / 2) - (editor
->width
() / 2),
rect.top() + (rect.height() / 2) - (editor->height() / 2));
editor->move(draw_at);
}
void CBItemDelegate
::paint(QPainter *painter,
bool data = index.model()->data(index, Qt::DisplayRole).toBool();
checkboxstyle.rect = option.rect;
checkboxstyle.rect.setLeft(option.rect.x() + option.rect.width()/2 - checkbox_rect.width()/2);
if(data)
checkboxstyle.
state = QStyle::State_On|QStyle
::State_Enabled;
else
checkboxstyle.
state = QStyle::State_Off|QStyle
::State_Enabled;
}
bool CBItemDelegate
::editorEvent(QEvent *event,
Q_ASSERT(event);
Q_ASSERT(model);
// make sure that the item is checkable
Qt::ItemFlags flags = model->flags(index);
if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
return false;
// make sure that we have a check state
QVariant value
= index.
data(Qt
::CheckStateRole);
if (!value.isValid())
return false;
// make sure that we have the right event type
if (event
->type
() == QEvent::MouseButtonRelease) { const int textMargin
= QApplication::style()->pixelMetric
(QStyle::PM_FocusFrameHMargin) + 1;
QRect checkRect
= QStyle::alignedRect(option.
direction, Qt
::AlignCenter,
check(option, option.rect, Qt::Checked).size(),
QRect(option.
rect.
x() + textMargin, option.
rect.
y(),
option.rect.width() - (2 * textMargin), option.rect.height()));
if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
return false;
} else if (event
->type
() == QEvent::KeyPress) { if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
&& static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
return false;
} else {
return false;
}
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
? Qt::Unchecked : Qt::Checked);
QString((state
== Qt
::Checked) ?
"Qt::Checked" : "Qt::Unchecked"),
QString("[%1/%2]").
arg(index.
row()).
arg(index.
column()));
return model->setData(index, state, Qt::CheckStateRole);
}
#include "cbitemdelegate.h"
#include <QApplication>
#include <QAbstractItemView>
#include <QtGui>
CBItemDelegate::CBItemDelegate(QTableView *parent)
: QItemDelegate(parent)
{
my_table = parent;
}
QWidget *CBItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex &index) const {
CheckBox *editor = new CheckBox(index.row(), parent);
editor->installEventFilter(const_cast<CBItemDelegate*>(this));
editor->setFixedSize(24, 24);
editor->setFocusPolicy(Qt::StrongFocus);
editor->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(editor, SIGNAL(pressed(int,Qt::CheckState)),
this, SLOT(slot_clicked(int,Qt::CheckState)),
Qt::DirectConnection);
QRect rect = my_table->visualRect(index);
QPoint draw_at(rect.left() + (rect.width() / 2) - (editor->width() / 2),
rect.top() + (rect.height() / 2) - (editor->height() / 2));
editor->move(draw_at);
return editor;
}
void CBItemDelegate::slot_clicked(int row, Qt::CheckState cs) {
emit sig_clicked(row, cs);
}
void CBItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const {
int value = index.model()->data(index, Qt::DisplayRole).toInt();
CheckBox *checkBox = static_cast<CheckBox*>(editor);
if(value == 1)
checkBox->setCheckState(Qt::Unchecked);
else
checkBox->setCheckState(Qt::Checked);
}
void CBItemDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const {
CheckBox *checkBox = static_cast<CheckBox*>(editor);
int value;
if(checkBox->checkState() == Qt::Checked)
value = 1;
else
value = 0;
model->setData(index, value);
}
void CBItemDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
QRect r;
r.setSize(QSize(24, 24));
r.moveTo((option.rect.width() / 2) - (editor->width() / 2),
(option.rect.height() / 2) - (editor->height() / 2));
editor->setGeometry(r);
QRect rect = my_table->visualRect(index);
QPoint draw_at(rect.left() + (rect.width() / 2) - (editor->width() / 2),
rect.top() + (rect.height() / 2) - (editor->height() / 2));
editor->move(draw_at);
}
void CBItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
bool data = index.model()->data(index, Qt::DisplayRole).toBool();
QStyleOptionButton checkboxstyle;
QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle);
checkboxstyle.rect = option.rect;
checkboxstyle.rect.setLeft(option.rect.x() + option.rect.width()/2 - checkbox_rect.width()/2);
if(data)
checkboxstyle.state = QStyle::State_On|QStyle::State_Enabled;
else
checkboxstyle.state = QStyle::State_Off|QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxstyle, painter);
}
bool CBItemDelegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index) {
Q_ASSERT(event);
Q_ASSERT(model);
// make sure that the item is checkable
Qt::ItemFlags flags = model->flags(index);
if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
return false;
// make sure that we have a check state
QVariant value = index.data(Qt::CheckStateRole);
if (!value.isValid())
return false;
// make sure that we have the right event type
if (event->type() == QEvent::MouseButtonRelease) {
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
check(option, option.rect, Qt::Checked).size(),
QRect(option.rect.x() + textMargin, option.rect.y(),
option.rect.width() - (2 * textMargin), option.rect.height()));
if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
return false;
} else if (event->type() == QEvent::KeyPress) {
if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
&& static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
return false;
} else {
return false;
}
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
? Qt::Unchecked : Qt::Checked);
QMessageBox::information(0,
QString((state == Qt::Checked) ? "Qt::Checked" : "Qt::Unchecked"),
QString("[%1/%2]").arg(index.row()).arg(index.column()));
return model->setData(index, state, Qt::CheckStateRole);
}
To copy to clipboard, switch view to plain text mode
Bookmarks