#include <QStyledItemDelegate>
#include <QPainter>
#include <QAbstractListModel>
#include <QStringList>
#include <QModelIndex>
#include <QLabel>
class mylistModel;
class mydeltgate : public QStyledItemDelegate
{
Q_OBJECT
public:
mydeltgate(QObject *parent = 0);
~mydeltgate();
protected:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const ;
/*注意事项:记得在末尾加const ,保持和基类虚函数的写法一致,不一致不会触发该函数*/
/*paint 需配合 sizehint一起使用*/
};
class mylistModel :public QAbstractListModel
{
Q_OBJECT
public:
explicit mylistModel(QObject *parent = 0);
~mylistModel();
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
void add(QString str);
private:
QStringList list;
int m_testNumber;
signals:
public slots :
};
cpp
mydeltgate::mydeltgate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
mydeltgate::~mydeltgate()
{
}
void mydeltgate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
QRect entityRect = option.rect;
QBrush t_brush1QColor(Qt::red);
painter->fillRect(entityRect, t_brush1QColor);
painter->save();
QRect rect1 = entityRect;
rect1.setTop(entityRect.top() + 20);
rect1.setBottom(entityRect.bottom() - 30);
rect1.setLeft(entityRect.left() + 20);
rect1.setRight(entityRect.right() - 20);
painter->drawRect(rect1);
painter->drawText(rect1.bottomLeft(), index.data(Qt::DisplayRole).toString());
painter->save();
QPainterPath path;
path.addRect(entityRect.x() + 30, entityRect.y()+30,30,30);
painter->setBrush(Qt::blue);
painter->drawPath(path);
painter->restore();//下次循环返回前一个save的painter
/*注意事项:这里的定位需用传进来的option.rect来做定位,不能用绝对定位*/
/*使用绝对定位时,每个index的数据都会重叠在该绝对定位*/
}
QSize mydeltgate::sizeHint(const QStyleOptionViewItem &option,const QModelIndex &index) const
{
return QSize(187 , 117 );
/*设置每行item的大小值*/
}
/*listmodel*/
mylistModel::mylistModel(QObject *parent) : QAbstractListModel(parent)
{
}
mylistModel::~mylistModel()
{
}
int mylistModel::rowCount(const QModelIndex &parent) const
{
/*行数*/
return list.count();
}
int mylistModel::columnCount(const QModelIndex &parent) const
{
/*列数*/
return 1;
}
QVariant mylistModel::data(const QModelIndex &index, int role) const
{
/*这个是主要函数,委托根据index的对应角色取这里的对应角色值*/
/*不设置委托时,DisplayRole返回值直接显示在界面上*/
if (role == Qt::BackgroundRole)
{
return QBrush(Qt::green);
}
else if (role == Qt::DisplayRole)
{
return list.at(index.row());
}
else
{
return QVariant();
}
}
void mylistModel::add(QString str)
{
list.append(str);
}
main
mylistModel* ItemModel = new mylistModel(this);
mydeltgate* delt = new mydeltgate(this);
QStringList strList;
strList.append("A");
strList.append("B");
strList.append("C");
strList.append("D");
strList.append("E");
strList.append("F");
strList.append("G");
int nCount = strList.size();
for (int i = 0; i < nCount; i++)
{
QString string = static_cast<QString>(strList.at(i));
ItemModel->add(string);
}
ui.listView->setItemDelegate(delt);
ui.listView->setModel(ItemModel);