16,203
社区成员
发帖
与我相关
我的任务
分享
#ifndef DELETETOOLBUTTONDELEGATE_H
#define DELETETOOLBUTTONDELEGATE_H
#include <QObject>
#include <QWidget>
#include <QItemDelegate>
#include <QMap>
#include <QStyleOptionButton>
#include <QModelIndex>
class DeleteToolButtonDelegate :public QItemDelegate
{
Q_OBJECT
public:
DeleteToolButtonDelegate(QObject *parent=0);
//自定义代理组件必须继承以下4个函数
//创建编辑组件
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;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex & index ) const Q_DECL_OVERRIDE;
private:
QMap<QModelIndex, QStyleOptionButton*> m_btns;
signals:
void deleteCommodityData() const;
};
#endif // DELETETOOLBUTTONDELEGATE_H
void DeleteToolButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton* button = m_btns.value(index);
if (!button) {
button = new QStyleOptionButton();
button->rect = option.rect.adjusted(0, 0, 0, 0);
//button->rect = QRect(option.rect.left(), option.rect.top(), option.rect.width(), option.rect.height());
//button->text = "X";
button->state |= QStyle::State_Enabled;
button->icon = QIcon(":/Image/sc.png");
button->iconSize = QSize(option.rect.width(), option.rect.height());
(const_cast<DeleteToolButtonDelegate*>(this))->m_btns.insert(index, button);
}
painter->save();
if (option.state & QStyle::State_Selected) {//被选中高亮
painter->fillRect(option.rect, option.palette.highlight());
}
painter->restore();
QApplication::style()->drawControl(QStyle::CE_PushButton, button, painter);
}