QTableView QAbstractTableModel QStyledItemDelegate QHeaderView 定制化表格(二)

不羁的心 提交于 2020-03-06 01:40:42

一、效果图
在这里插入图片描述
二、工程构建思路
依据mvc模型,重写QTableView QAbstractTableModel QStyledItemDelegate QHeaderView,利用QStyleOptionProgressBar自定义进度条样式,重写createEditor等必要函数实现自定义编辑输入框。
三、代码片段

//样式代理部分
#include "tabledelegate.h"

TableDelegate::TableDelegate(QObject *parent):
    QStyledItemDelegate(parent)
{

}

void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //去掉Focus
    QStyleOptionViewItem viewOption(option);
    initStyleOption(&viewOption, index);
    if (option.state.testFlag(QStyle::State_HasFocus))
        viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
    QStyledItemDelegate::paint(painter, viewOption, index);

    //绘制进度条
    if(index.column() == 7){
        int nProgress = index.model()->data(index, Qt::DisplayRole).toInt();
        int nLeft = 8;
        int nTop = 8;
        int nWidth = option.rect.width() - 2 * nLeft;
        int nHeight = option.rect.height() - 2 * nTop;
        QStyleOptionProgressBar progressBarOption;
        progressBarOption.initFrom(option.widget);
        progressBarOption.rect = QRect(option.rect.left() + nLeft, option.rect.top() + nTop,  nWidth, nHeight);
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.textAlignment = Qt::AlignCenter;
        progressBarOption.progress = nProgress;
        progressBarOption.text = QString("%1%").arg(nProgress);
        progressBarOption.textVisible = true;
        option.widget->style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
        return;
    }
}

QWidget *TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.column() == 5){
        QLineEdit *edit = new QLineEdit(parent);
        edit->setPlaceholderText(QString::fromLocal8Bit("请输入"));
        return edit;
    }
    return createEditor(parent,option,index);
}

void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (index.column() == 5) {
        QString txt = index.model()->data(index, Qt::DisplayRole).toString();
        QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
        edit->setText(txt);
        return;
    }
    QStyledItemDelegate::setEditorData(editor, index);
}

void TableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (index.column() == 5) {
        QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
        QString txt = edit->text();
        model->setData(index, txt);
    } else {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}

void TableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect.adjusted(5,5,-10,0));
}

bool TableDelegate::eventFilter(QObject *object, QEvent *event)
{
    return QStyledItemDelegate::eventFilter(object,event);
}

bool TableDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    return QStyledItemDelegate::editorEvent(event,model,option,index);
}

//数据model部分
#include "tablemodel.h"

TableModel::TableModel(QObject *parent):
    QAbstractTableModel(parent)
{
    for(int j=0;j<35;j++){
        QStringList list;
        list.append(QString::number(j));
        list.append("FK"+QString::number(j));
        list.append("V"+QString::number(j));
        list.append(QString::fromLocal8Bit("修模"));
        list.append(QString::fromLocal8Bit("加工中"));
        list.append(QString::fromLocal8Bit("90%"));
        list.append(QString::fromLocal8Bit("江苏"));
        list.append(QString::fromLocal8Bit("75"));

        m_vData.append(list);
    }
}


int TableModel::columnCount(const QModelIndex &parent) const
{
    return m_vData.at(0).size();
}

int TableModel::rowCount(const QModelIndex &parent) const
{
    return m_vData.size();
}

QVariant TableModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    if(Qt::TextAlignmentRole == role)
        return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);
    if(Qt::DisplayRole == role)
        return m_vData.value(index.row()).value(index.column());

    return QVariant();
}

bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(index.column() == 5 && role == Qt::EditRole){
        QStringList list = m_vData.at(index.row());
        list.replace(index.column(),value.toString());
        m_vData.replace(index.row(),list);
        emit dataChanged(index,index);
        return true;
    }
    return QAbstractTableModel::setData(index,value,role);
}

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    QStringList list;
    list.append(QString::fromLocal8Bit("序号"));
    list.append(QString::fromLocal8Bit("模具编号"));
    list.append(QString::fromLocal8Bit("版本号"));
    list.append(QString::fromLocal8Bit("类型"));
    list.append(QString::fromLocal8Bit("状态"));    
    list.append(QString::fromLocal8Bit("合格率"));
    list.append(QString::fromLocal8Bit("地区"));
    list.append(QString::fromLocal8Bit("进度"));


    if(orientation == Qt::Horizontal)
    {
        switch(role)
        {
        case Qt::TextAlignmentRole:
            return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);

        case Qt::DisplayRole:
            return list.at(section);
        default:
            return QVariant();
        }
    }
    return QAbstractTableModel::headerData(section,orientation,role);
}

bool TableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    return QAbstractTableModel::setHeaderData(section,orientation,value,role);
}

Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
    if(index.column() == 5){
        Qt::ItemFlags flag = QAbstractTableModel::flags(index);
        flag |= Qt::ItemIsEditable;
        return flag;
    }
    return QAbstractTableModel::flags(index);
}

e-mail 519096571@qq.com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!