這是代碼
#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);
}
uj5u.com熱心網友回復:

uj5u.com熱心網友回復:
就是洗掉那個小圖示,不會隨著滑動攔移動uj5u.com熱心網友回復:
謝謝??,各位大佬轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/260788.html
標籤:Qt
上一篇:尋最新微信強制分享紅包原始碼
