.h文件
// QComboBox 委托
class ComBoxDelegate final : public QItemDelegate
{
Q_OBJECT
public:
ComBoxDelegate(const QStringList& items, QObject* parent = 0);
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
void setEditorData(QWidget* editor, const QModelIndex& index) const override;
void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
void updateEditorGeometry(QWidget* editor,const QStyleOptionViewItem& option, const QModelIndex& index) const override;
signals:
void ValueChanged(const QString& text) const;
private:
QStringList m_items;
};
.cpp实现
ComBoxDelegate::ComBoxDelegate(const QStringList& items, QObject* parent):QItemDelegate(parent),
m_items(items)
{
}
void ComBoxDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionViewItem viewOption(option);
QStyle* style = viewOption.widget ? viewOption.widget->style() : QApplication::style();
// 绘制三角形箭头
QStyleOptionComboBox comboBoxOption;
comboBoxOption.rect = option.rect.adjusted(option.rect.width() - 20, 0, 0, 0);
comboBoxOption.palette = viewOption.palette;
comboBoxOption.state = QStyle::State_Enabled | QStyle::State_Active;
//comboBoxOption.currentText = "▼";
//style->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter, viewOption.widget);
// 自定义三角形箭头样式
painter->save();
painter->setPen(Qt::black);
painter->setBrush(Qt::black);
QPoint arrowPoints[3] = {
QPoint(comboBoxOption.rect.center().x() - 4, comboBoxOption.rect.center().y() - 2),
QPoint(comboBoxOption.rect.center().x() + 4, comboBoxOption.rect.center().y() - 2),
QPoint(comboBoxOption.rect.center().x(), comboBoxOption.rect.center().y() + 2)
};
painter->drawPolygon(arrowPoints, 3);
painter->restore();
QItemDelegate::paint(painter, viewOption, index);
}
QWidget* ComBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QComboBox* editor = new QComboBox(parent);
editor->addItems(m_items);
connect(editor, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), [=](const QString& text) {
emit ValueChanged(text);
});
return editor;
}
void ComBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox* combobox = static_cast<QComboBox*>(editor);
combobox->setCurrentText(value);
combobox->showPopup();
}
void ComBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QComboBox* comboBox = static_cast<QComboBox*>(editor);
QString text = comboBox->currentText();
model->setData(index, text, Qt::EditRole);
//emit ValueChanged(text);
}
void ComBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
editor->setGeometry(option.rect);
}
// 调用测试
ComBoxDelegate* pCombox = new ComBoxDelegate(EventTypes, ui->tableView_commprop);
connect(pCombox, &ComBoxDelegate::ValueChanged, this, [&](const QString& text) {
SetEventItemData(ui->tableView_commprop->currentIndex(), text);
});
ui->tableView_commprop->setItemDelegateForColumn(0, pCombox);