#include "mydelegate.h"
#include <QtWidgets>
int m_indexflush = -1;
MyDelegate::MyDelegate(QObject*parent):QStyledItemDelegate(parent)
{
close_hightlight_index = -1;
}
void MyDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QString picstr = index.data(Qt::DecorationRole).toString();
if(picstr.isEmpty()) return;
QPixmap pixmap(picstr);
QRect rect = option.rect;
QRect picrect;
picrect = QRect(rect.left()+(rect.width()-pixmap.width())/2,rect.top()+(rect.height()-pixmap.height())/2,pixmap.width(),pixmap.height());
painter->drawPixmap(picrect,pixmap);
painter->save();
if( option.state&QStyle::State_MouseOver || (m_indexflush == index.row()))
{
painter->setPen(QPen(Qt::lightGray));
painter->drawRect(rect.left(),rect.top(),rect.width()-1,rect.height()-1);
if(close_hightlight_index == index.row())
{
QPixmap closepic("F:\\myproj\\QML\\TestListModel_Update\\close.png");
QRect closerect = QRect(rect.left()+rect.width()-closepic.width()-1,rect.top()+1,closepic.width(),closepic.height());
painter->drawPixmap(closerect,closepic);
}
else
{
QPixmap closepic("F:\\myproj\\QML\\TestListModel_Update\\close_a.png");
QRect closerect = QRect(rect.left()+rect.width()-closepic.width()-1,rect.top()+1,closepic.width(),closepic.height());
painter->drawPixmap(closerect,closepic);
}
m_indexflush = -1;
}
else
{
}
painter->restore();
}
bool MyDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index)
{
QRect rect = option.rect;
QPixmap closepic("F:\\myproj\\QML\\TestListModel_Update\\close.png");
QRect closerect = QRect(rect.left()+rect.width()-closepic.width()-1,rect.top()+1,closepic.width(),closepic.height());
QMouseEvent *mouseevent = static_cast<QMouseEvent*>(event);
if(!mouseevent) return false;
if(mouseevent->type() == QEvent::MouseButtonRelease && closerect.contains(mouseevent->pos()))
{
m_indexflush = index.row();
emit signal_deleteclick(index);
}
if(mouseevent->type() == QEvent::MouseMove)
{
if(closerect.contains(mouseevent->pos()) )
{
close_hightlight_index = index.row();
emit signal_mousemovedel(index);
}
else
{
close_hightlight_index = -1;
emit signal_mousemovedel(QModelIndex());
}
qDebug()<<close_hightlight_index;
}
return QStyledItemDelegate::editorEvent(event,model,option,index);
}
Qt 自定义Model和自定义Delegate
4星 · 超过85%的资源 需积分: 38 25 浏览量
2016-11-21
16:37:35
上传
评论 2
收藏 26KB RAR 举报
keaiyao
- 粉丝: 0
- 资源: 5
最新资源
- STM32-Lib-LIS3DSH-Accelerometer-main
- 软件测试面试题.pdf
- WINSOFT ComPort 6.0 for Delphi XE10.1-XE10.3 Cracked
- 数据库基础知识参考试题.doc
- 数据库存储引擎技术的优劣势分析.docx
- 基于GPT的AI文档分析、阅读和问答工具.txt
- 《机器人控制系统的设计与Matlab仿真 》仿真程序
- AI-免费物品无损放大工具AI在线免费放大图片工具.txt
- C++基于DPLL算法的SAT的蜂窝数独游戏求解程序,程序设计综合课程设计,包括SAT求解器板块、蜂窝数独转化成cnf公式板块
- 微信小程序恐龙快跑小程序源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈