模型视图中的委托

QAbstractItemDelegate 类用于显示和编辑模型中的数据项。
QAbstractItemDelegate 为模型/视图架构中的委托提供接口和通用功能。 委托在视图中显示单个项目,并处理模型数据的编辑。
QAbstractItemDelegate 类是模型/视图类之一,是 Qt 模型/视图框架的一部分。
要以自定义方式呈现项目,您必须实现paint() 和sizeHint()。 QItemDelegate 类为这些函数提供了默认实现; 如果您不需要自定义渲染,请改为对该类进行子类化。

 运行效果图:

 源码如下:

QWidget* CustomizedItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QWidget* ret = NULL;
    
    m_index = index;
    if(QVariant::Bool == index.data(Qt::DisplayRole).type())
    {
        QCheckBox* check = new QCheckBox(parent);
        
        check->setText(QString::fromLocal8Bit("文官"));
        
        ret = check;
    }
    else if(QVariant::Char == index.data(Qt::DisplayRole).type())
    {
        QComboBox* combo = new QComboBox(parent);
        
        combo->addItem("A");
        combo->addItem("B");
        combo->addItem("C");
        combo->addItem("D");
        combo->addItem("E");
        
        ret = combo;
    }
    else
    {
        ret = QItemDelegate::createEditor(parent, option, index);
    }
    
    return ret;
}

void CustomizedItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(index);
    editor->setGeometry(option.rect);
}

void CustomizedItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if(QVariant::Bool == index.data(Qt::DisplayRole).type())
    {
        QCheckBox* check = qobject_cast<QCheckBox*>(editor);
        
        if(check != NULL)
        {
            check->setChecked(index.data().toBool());
        }
    }
    else if(QVariant::Char == index.data(Qt::DisplayRole).type())
    {
        QComboBox* combo = dynamic_cast<QComboBox*>(editor);
        
        if(NULL != combo)
        {
            for(int i=0; i<combo->count(); i++)
            {
                if(combo->itemText(i) == index.data().toString())
                {
                    combo->setCurrentIndex(i);
                    break;
                }
            }
        }
    }
    else
    {
        QItemDelegate::setEditorData(editor, index);
    }
}

void CustomizedItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if(QVariant::Bool == index.data(Qt::DisplayRole).type())
    {
        QCheckBox* check = qobject_cast<QCheckBox*>(editor);
        
        if(NULL != check)
        {
            model->setData(index, check->isChecked(), Qt::DisplayRole);
        }
    }
    else if(QVariant::Char == index.data(Qt::DisplayRole).type())
    {
        QComboBox* combo = dynamic_cast<QComboBox*>(editor);
        
        if(NULL != combo)
        {
            model->setData(index, combo->currentText().at(0), Qt::DisplayRole);
        }
    }
    else
    {
        QItemDelegate::setModelData(editor, model, index);
    }
}

void CustomizedItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if(m_index != index)
    {
        QItemDelegate::paint(painter, option, index);
    }
}

void CustomizedItemDelegate::OnCloseEditor()
{
    m_index = QModelIndex();
}

源码:https://download.csdn.net/download/weixin_41937297/35656492

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值