您将必须创建一个自定义 itemDelegate
为您 QListView
.
您可以子类 QItemDelegate
创建自己的代表类。利用 sizeHint
功能以设置分离器的大小并将其涂在 paint
功能。检查项目是否是分离器 index.data(Qt::AccessibleDescriptionRole).toString()
.
#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H
#include <QItemDelegate>
class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ComboBoxDelegate(QObject *parent = 0);
protected:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
#endif // COMBOBOXDELEGATE_H
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator"))
{
painter->setPen(Qt::red);
painter->drawLine(option.rect.left(), option.rect.center().y(), option.rect.right(), option.rect.center().y());
}
else
QItemDelegate::paint(painter, option, index);
}
QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString type = index.data(Qt::AccessibleDescriptionRole).toString();
if(type == QLatin1String("separator"))
return QSize(0, 2);
return QItemDelegate::sizeHint( option, index );
}
然后只是将您的自定义代表设置为您的 listView
:
listView->setItemDelegate(new ComboBoxDelegate);
.