#include "checkboxdelegate.h"
#include <QDebug>
#include <QTextOption>
#include <QPainter>
#include <QCheckBox>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>
#include <QRect>
#include <QPoint>
#include <QPalette>
#include <QPixmapCache>
#include <QFileIconProvider>
#include <QIcon>
#include <QPixmap>
#define CHECK_BOX_COLUMN 0
#define File_PATH_COLUMN 1
//static QRect CheckBoxRect(const QStyleOptionViewItem &viewItemStyleOptions)/*const*/
//{
// //绘制按钮所需要的参数
// QStyleOptionButton checkBoxStyleOption;
// //按照给定的风格参数 返回元素子区域
// QRect checkBoxRect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkBoxStyleOption);
// //返回QCheckBox坐标
// QPoint checkBoxPoint(viewItemStyleOptions.rect.x() + viewItemStyleOptions.rect.width() / 2 - checkBoxRect.width() / 2,
// viewItemStyleOptions.rect.y() + viewItemStyleOptions.rect.height() / 2 - checkBoxRect.height() / 2);
// //返回QCheckBox几何形状
// return QRect(checkBoxPoint, checkBoxRect.size());
//}
CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
:QStyledItemDelegate(parent)
{
m_pCheckBox = new QCheckBox;
m_pCheckBox->setFixedSize(qRound(1.3 * m_pCheckBox->sizeHint().height()), m_pCheckBox->sizeHint().height());
m_pCheckBox->setStyleSheet("QCheckBox::indicator:unchecked {image: url(:/res/unchecked.png);} \
QCheckBox::indicator:unchecked:hover {image: url(:/res/uncheckedHover.png);} \
QCheckBox::indicator:unchecked:pressed {image: url(:/res/uncheckedPressing.png);} \
QCheckBox::indicator:checked {image: url(:/res/checked.png);} \
QCheckBox::indicator:checked:hover {image: url(:/res/checkedHover.png);} \
QCheckBox::indicator:checked:pressed {image: url(:/res/checkedPressing.png);} \
QCheckBox::indicator:indeterminate {image: url(:/res/partiallyChecked.png);} \
QCheckBox::indicator:indeterminate:hover {image: url(:/res/partiallyCheckedHover.png);} \
QCheckBox::indicator:indeterminate:pressed {image: url(:/res/partiallyCheckedPressing.png);}");
qDebug() << "CheckBoxDelegate::new ===========CheckBoxDelegate====m_pCheckBox==" << m_pCheckBox << sizeof(*m_pCheckBox);
m_pLabel = new QLabel;
m_pLabel->setWordWrap(false);
}
CheckBoxDelegate::~CheckBoxDelegate()
{
delete m_pCheckBox;
m_pCheckBox = NULL;
delete m_pLabel;
m_pLabel = NULL;
qDebug() << "CheckBoxDelegate::~CheckBoxDelegate====m_pCheckBox==" << m_pCheckBox;
}
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == CHECK_BOX_COLUMN)
{
qDebug() << "CheckBoxDelegate::paint=====column================" << index.column();
bool selected = option.state & QStyle::State_Selected;
QPalette palette(option.palette);
palette.setColor(QPalette::Active, QPalette::Window,
selected ? option.palette.highlight().color()
: option.palette.base().color());
palette.setColor(QPalette::Active, QPalette::WindowText,
selected
? option.palette.highlightedText().color()
: option.palette.text().color());
int yOffset = m_pCheckBox->height() < option.rect.height()
? (option.rect.height() - m_pCheckBox->height()) / 2 : 0;
QRect checkboxRect(option.rect.x(), option.rect.y() + yOffset,
m_pCheckBox->width(), m_pCheckBox->height());
m_pCheckBox->setPalette(palette);
int checked = index.model()->data(index, Qt::CheckStateRole).toInt();
if(0 == checked)
{
m_pCheckBox->setCheckState(Qt::Unchecked);
}
else if(1 == checked)
{
m_pCheckBox->setCheckState(Qt::PartiallyChecked);
}
else
{
m_pCheckBox->setCheckState(Qt::Checked);
}
QRect labelRect(option.rect.x() + m_pCheckBox->width(),option.rect.y(), \
option.rect.width() - m_pCheckBox->width(), option.rect.height());
m_pLabel->setPalette(palette);
m_pLabel->setFixedSize(qMax(0, labelRect.width()), labelRect.height());
QString html = index.model()->data(index, Qt::DisplayRole).toString();
m_pLabel->setText(html);
QString checkboxKey = QString("CHECKBOX:%1.%2").arg(selected).arg(checked);
paintWidget(painter, checkboxRect, checkboxKey, m_pCheckBox);
QString labelKey = QString("LABEL:%1.%2.%3x%4").arg(selected).arg(html).arg(labelRect.width()).arg(labelRect.height());
//paintWidget(painter, labelRect, labelKey, m_pLabel);
//1、获取文件夹图标
QFileIconProvider icon_provider;
QIcon icon = icon_provider.icon(QFileIconProvider::Folder);
QPixmap pix = icon.pixmap(QSize(20, 20));//QIcon转QPixmap
//qDebug() << "labelRect============" << labelRect;
// 2、获取指定文件图标
// QFileInfo file_info(name);
// QFileIconProvider icon_provider;
// QIcon icon = icon_provider.icon(file_info);
QRect iconRect(option.rect.x() + m_pCheckBox->width(), option.rect.y() + 5, 20, 20);
painter->drawPixmap(iconRect, pix);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
void CheckBoxDelegate::paintWidget(QPainter *painter, const QRect &rect, const QString &cacheKey, QWidget *widget) const
{
QPixmap pixmap(widget->size());
#if QT_VERSION >= 0x040600
if (!QPixmapCache::find(cacheKey, &pixmap)) {
#else
if (!QPixmapCache::find(cacheKey, pixmap)) {
#endif
widget->render(&pixmap);
QPixmapCache::insert(cacheKey, pixmap);
}
painter->drawPixmap(rect, pixmap);
}
//
bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
qDebug() << "CheckBoxDelegate::editorEvent=====data================" << index.column();
QRect decorationRect = option.rect;
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if(event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
{
if(index.column() == CHECK_BOX_COLUMN)
{
int data = model->data(index, Qt::CheckStateRole).toInt();
qDebug() << "data================" << data;
if(data == 0)
data = 2;
else
data = 0;
model->setData(index, data, Qt::CheckStateRole);
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
/*
QWidget *CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug() << "index.column===============" << index.column();
if(index.column() == 0)
{
QCheckBox *checkbox = new QCheckBox(parent);
return checkbox;
}
else
{
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(index.column() == 0)
{
bool check = index.model()->data(index, Qt::UserRole).toBool();
qDebug() << "check ============ " << check;
QCheckBox *checkbox = qobject_cast<QCheckBox *>(editor);
checkbox->setChecked(check);
}
}
void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.column() == 0)
{
QCheckBox *checkbox = qobject_cast<QCheckBox *>(editor);
bool check = checkbox->isChecked();
model->setData(index, check, Qt::UserRole);
}
}
void CheckBoxDelegate::
QTableView同时显示自定义Checkbox和文件图标
需积分: 45 177 浏览量
2018-08-26
22:09:18
上传
评论
收藏 1.16MB ZIP 举报
班公湖里洗过脚
- 粉丝: 571
- 资源: 18
最新资源
- java +elasticsearch+倒排索引技术+PUT+GET+全文搜索、短语匹配、近似搜索和聚合等操作高效执行
- ES数据迁移~~~~~~~~~~~~~~
- E3P303-VB一款SOP8封装2个P-Channel场效应MOS管
- SpyGlass-LowPowerRules-Reference
- 基于Java的SSM框架矿建环境搭建设计源码
- SpyGlass-PowerVerifyAdvancedRules-Reference
- java作业管理系统设计(源代码+论文).rar
- 奥迪C6仪表开机报文··
- SpyGlass-PowerEstimate-And-SpyGlass-PowerReduceRules-Reference
- E3P102-VB一款SOP8封装2个P-Channel场效应MOS管
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈