• QTableView


    #include "ModelTable.h"
    #include "DaRecord.h"
    #include "ScopeTailed.h"


    ModelTable::ModelTable()
    {
    header_ <<.....;
    }


    ModelTable::~ModelTable()
    {
    }

    QVariant ModelTable::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
    {
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    return header_[section];
    return QVariant();
    }

    QVariant ModelTable::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const
    {
    if (!index.isValid())
    return QVariant();
    if (role == Qt::CheckStateRole) //..勾选
    {
    if (index.column() == 0)
    {
    if (checkStateMap_.contains(index.row()))
    return checkStateMap_[index.row()] ? Qt::Checked : Qt::Unchecked;
    return Qt::Unchecked;
    }
    }
    if (role==Qt::DisplayRole)
    {
    RecordTree res = res_[index.row()];
    switch (index.column())
    {
    case 0:return res.spmc_;
    case 1:return res.zs_;
    case 2:return res.slv_.toString();
    }
    }
    return QVariant();
    }

    int ModelTable::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
    {
    return res_.count();
    }

    int ModelTable::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
    {
    return header_.count();
    }

    void ModelTable::loadData()
    {
    beginResetModel();
    res_;
    endResetModel();
    }

    void ModelTable::loadData(RecordTree &res)
    {
    beginResetModel();
    res_.clear();
    res_ << res;
    checkStateMap_.clear();
    endResetModel();
    }

    Qt::ItemFlags ModelTable::flags(const QModelIndex &index) const
    {
    const Qt::ItemFlags editable = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    const Qt::ItemFlags checkedtable = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
    if (index.column() == 0)
    return checkedtable;
    if (index.isValid())
    {
    switch (index.column())
    {
    case 0:return editable;
    case 1:return editable;
    case 2:return editable;
    }
    }
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    }

    bool ModelTable::setData(const QModelIndex &index, const QVariant &value, int role /* = Qt::EditRole */)
    {
    if (!index.isValid() && index.row() >= res_.count())
    return false;
    if (role == Qt::CheckStateRole && index.column() == 0)
    checkStateMap_[index.row()] = (value == Qt::Checked ? Qt::Checked : Qt::Unchecked);

    RecordTree & record = res_[index.row()];
    //editable (可编辑)
    if (index.isValid() && role == Qt::EditRole)
    {
    if (index.column() == 0)
    record.zs_ = value.toString();
    else if (index.column() == 1)
    record.slv_ = value.toString();
    }
    //refresh full row (刷新全部行)
    emit dataChanged(createIndex(index.row(), 0), createIndex(index.row(), header_.count()));
    return true;
    }

    void ModelTable::Clear()
    {
    checkStateMap_.clear();
    res_.clear();
    }

    技术是一张窗户纸,一捅就破,come!
  • 相关阅读:
    排序算法<四>希尔排序
    排序算法<三>快速排序
    排序算法<二>冒泡排序
    排序算法<一>选择排序
    相信我这是水题ny1313
    斐波那契查找
    插值查找
    第四届蓝桥杯c/c++B组4
    如何使用NPM来管理你的Node.js依赖
    javascript深入学习
  • 原文地址:https://www.cnblogs.com/adeng-x/p/8097895.html
Copyright © 2020-2023  润新知