Qt自定义表格代理

  1. 头文件 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

  1. 源文件 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);
    }
}

  1. 实例
...
//设置代理
    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));
...
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值