借助treeView和tableView演示MVC的使用

借助treeView和tableView演示MVC的使用

tableView和treeView可联动(互相控制)


委托:
 1.
     单元格可编辑(QLineEdit),可根据下拉列表选择显示内容(QComboBox)
  重载createEditor创建编辑器
  重载setModelData设置模型数据
  重载setEditorData设置编辑器数据

#include "delegate_edit.h"
#include <QLineEdit>
#include <QComboBox>
#include "model.h"

Delegate_edit::Delegate_edit(QObject *parent) :
    QStyledItemDelegate(parent)
{
    this->m_style<<"qq"<<"fetion"<<"explorer"<<"dictionary"<<"unknown";
}

QWidget *Delegate_edit::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(!index.isValid())return 0;
    Q_UNUSED(option);
    switch(index.column())
    {
    case 1:return new QLineEdit(parent);//创建编辑器
    case 2:{QComboBox *comboBox = new QComboBox(parent);comboBox->addItems(this->m_style); return comboBox;}
    default:return 0;
    }
    return 0;
}

void Delegate_edit::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if(!index.isValid())return;
    switch(index.column())
    {
    case 1:((Model*)model)->setData(index,((QLineEdit*)editor)->text(),Qt::EditRole);break;//创建编辑器,设置数据源
    case 2:((Model*)model)->setData(index,((QComboBox*)editor)->currentText(),Qt::EditRole);break;
    default:break;
    }
}

void Delegate_edit::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if(!index.isValid())return;

    switch(index.column())
    {
    case 1:((QLineEdit*)editor)->setText(index.model()->data(index).toString());break;//创建编辑器,显示数据
    case 2:((QComboBox*)editor)->setCurrentIndex(this->m_style.indexOf(index.model()->data(index).toString()));break;//注意QComboBox当前索引的计算
    default:break;
    }
}


 

 2.
     单元格显示进度条(重绘)
  重载paint函数
  重载displayText(

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值