实现如下所示的下拉框," + ", " - "一直显示在·最后两个。
利用代理实现
class TextWgt : public QWidget
{
Q_OBJECT
public:
explicit TextWgt (QWidget *parent = nullptr);
//添加模型
void addItem(const QString &text);
private:
void init();
private:
QListView* m_pView;
ItemDelegate* m_pItemDelegate; //代理
QStandardItemModel* m_pModel;
QComboBox* m_pCbx;
};
TextWgt::TextWgt(QWidget *parent)
: QWidget(parent)
{
init();
}
void TextWgt::addItem(const QString &text)
{
QStandardItem * item = new QStandardItem(text);
m_pModel->insertRow(m_pModel->rowCount() - 2, item);
m_pCbx->setCurrentText(text);
}
void TextWgt::init()
{
m_pCbx = new QComboBox(this);
m_pModel = new QStandardItemModel(this);
m_pCbx->setModel(m_pModel);
m_pView = new QListView(this);
m_pCbx->setView(m_pView);
//代理
m_pItemDelegate = new ItemDelegate;
m_pView->setItemDelegate(m_pItemDelegate);
//添加项---“+”
QStandardItem * addItem = new QStandardItem();
m_pModel->blockSignals(true);
m_pModel->appendRow(addItem);
m_pModel->blockSignals(false);
//删除项---“-”
QStandardItem * deleteItem = new QStandardItem();
m_pModel->blockSignals(true);
m_pModel->appendRow(deleteItem);
m_pModel->blockSignals(false);
QHBoxLayout * layout = new QHBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->addWidget(m_pCbx);
}
代理代码如下:
class ItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ItemDelegate(QObject *parent = nullptr);
protected:
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
ItemDelegate::ItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.isValid())
{
//文本
QString text = index.data(Qt::EditRole).toString();
QRect rect = option.rect.adjusted(-1, 0, -1, 0);
//最后一列才画 +
if(index.row() == (index.model()->rowCount() - 1))
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QColor("#1A3C5F"));
painter->drawRect(rect);
painter->restore();
if(option.state.testFlag(QStyle::State_MouseOver))
{
//绘制背景色
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QColor("#3786DA"));
painter->drawRect(rect);
painter->restore();
}
painter->save();
int x = option.rect.width() / 2;
int y = option.rect.top() + option.rect.height() / 2;
QPoint startX(x - 9, y);
QPoint startY(x + 9, y);
painter->drwLine(startX, startY);
QPoint endX(x, y - 9);
QPoint endY(x, y + 9);
painter->drwLine(endX, endY);
painter->restore();
}
//"-号"
else if(index.row() == (index.model()->rowCount() - 2))
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QColor("#1A3C5F"));
painter->drawRect(rect);
painter->restore();
if(option.state.testFlag(QStyle::State_MouseOver))
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QColor("#3786DA"));
painter->drawRect(rect);
painter->restore();
}
painter->save();
painter->setPen(QPen(Qt::white, 2));
int x = option.rect.width() / 2 - 9;
int y = option.rect.top() + option.rect.height() / 2;
QPoint start(x, y);
QPoint end(x + 18, y);
painter->drawLine(start, end);
painter->restore();
}
else
{
//选中和未选中背景色不同
painter->save();
painter->setPen(Qt::NoPen);
if (option.state.testFlag(QStyle::State_Selected))
painter->setBrush(QColor("#3786DA"));
else
painter->setBrush(QColor("#1A3C5F"));
painter->drawRect(rect);
painter->restore();
//选中和未选中字体颜色都是白色
painter->save();
painter->setPen(Qt::white);
QFont font;
font.setPixelSize(13);
font.setFamily("Miscrosoft YaHei");
painter->setFont(font);
painter->drawText(rect, Qt::AlignLeft | Qt::AlignCenter, text);
painter->restore();
}
}
// QStyledItemDelegate::paint(painter, option, index);
}