- 头文件 editDelegate.h
#ifndef EDITDELEGATE_H
#define EDITDELEGATE_H
#include <QStyledItemDelegate>
#include <QItemDelegate>
#include <QModelIndex>
#include <QPainter>
#include <QWidget>
class EditDelegate: public QItemDelegate
{
Q_OBJECT
public:
enum itemType {
NameLineEditor = 0,
IpLineEditor = 1,
ComboBox = 2,
MacLineEditor = 3
};
public:
EditDelegate(int column, int itemType, QObject *parent=nullptr, QStringList textList=QStringList());
~EditDelegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
private:
int column = 0;
int itemType = 0;
QStringList textList = QStringList();
};
#endif // EDITDELEGATE_H
- 源文件 editDelegate.cpp
#include "editDelegate.h"
#include <QComboBox>
#include <QLineEdit>
EditDelegate::EditDelegate(int column, int itemType, QObject *parent, QStringList textList)
: QItemDelegate(parent)
{
this->column = column;
this->itemType = itemType;
this->textList = textList;
}
EditDelegate::~EditDelegate()
{
}
void EditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QItemDelegate::paint(painter, option, index);
}
QSize EditDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QItemDelegate::sizeHint(option, index);
}
QWidget *EditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.isValid() && index.column() == this->column)
{
if (this->itemType == ComboBox)
{
QComboBox *editor = new QComboBox(parent);
editor->setStyleSheet("background:#F8F8FF; color:#EE7621;");
editor->setEditable(false);
editor->installEventFilter(const_cast<EditDelegate *>(this));
return editor;
}
else if (this->itemType == IpLineEditor)
{
QLineEdit *editor = new QLineEdit(parent);
QRegExp regIp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)(\\/\\d{1,2})?");
editor->setValidator(new QRegExpValidator(regIp, editor));
editor->installEventFilter(const_cast<EditDelegate *>(this));
return editor;
}
else if (this->itemType == NameLineEditor)
{
QLineEdit *editor = new QLineEdit(parent);
QRegExp regName("[\\w\\d_\\.-@]{1,31}");
editor->setValidator(new QRegExpValidator(regName, editor));
editor->installEventFilter(const_cast<EditDelegate *>(this));
return editor;
}
else if (this->itemType == MacLineEditor)
{
QLineEdit *editor = new QLineEdit(parent);
QRegExp regMac("([0-9A-Fa-f]{2})([:-]?[0-9A-Fa-f]{2}){5}");
editor->setValidator(new QRegExpValidator(regMac, editor));
editor->installEventFilter(const_cast<EditDelegate *>(this));
return editor;
}
}
return QItemDelegate::createEditor(parent, option, index);
}
void EditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (index.isValid() && index.column() == this->column && this->itemType == ComboBox)
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *combox = static_cast<QComboBox *>(editor);
combox->addItems(this->textList);
combox->setCurrentText(value);
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void EditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.isValid() && index.column() == this->column && this->itemType == ComboBox)
{
QComboBox *combox = static_cast<QComboBox *>(editor);
model->setData(index, combox->currentText());
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
- 实例
...
//设置代理
ui->tb_subAddr->setItemDelegateForColumn(0, new EditDelegate(0, EditDelegate::NameLineEditor, this));
ui->tb_subAddr->setItemDelegateForColumn(1, new EditDelegate(1, EditDelegate::NameLineEditor, this));
ui->tb_subAddr->setItemDelegateForColumn(2, new EditDelegate(2, EditDelegate::IpLineEditor, this));
ui->tb_subAddr->setItemDelegateForColumn(3, new EditDelegate(3, EditDelegate::MacLineEditor, this));
QStringList textList;
textList.clear();
textList << "A" << "B" << "C";
EditDelegate *subNet_cbx = new EditDelegate(4, EditDelegate::ComboBox, this, textList);
ui->tb_subAddr->setItemDelegateForColumn(4, subNet_cbx);
ui->tb_subAddr->setItemDelegateForColumn(5, new EditDelegate(5, EditDelegate::IpLineEditor, this));
...