【Qt】QAbstractTableModel + QHeaderView自定义Table表头显示,无法显示表头内容,QHeaderView无法绘制内容的问题

问题:

QAbstractTableModel + QHeaderView自定义Table表头显示,无法显示表头内容,QHeaderView无法绘制内容的问题。
在这里插入图片描述

问题:

我编写时遇到的问题:在重载QAbstractTableModel::headerData()时出错。
异常代码:

QVariant FFileTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    QString value;
    switch (section)
    {
    case 0: value = "a"; break;
    case 1: value = "b"; break;
    case 2: value = "c"; break;
    case 3: value = "d"; break;
    case 4: value = "e"; break;
    case 5: value = "f"; break;
    case 6: value = "g"; break;
    default: break;
    }
    return value;
}

该代码准备用于展示Table表头的文案。且自定义QHeaderView内容也贴出来:

void FFileTableHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();

    painter->setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter->setBrush(QColor(0x30, 0x31, 0x34));
    painter->setPen(QPen(QColor(0x30, 0x31, 0x34), 0));
    painter->drawRect(rect);

    // 绘制文字
    QRect geometry(rect.left() + 15, 6, 25, 18);
    QString text = this->fileTableModel ? this->fileTableModel->headerData(logicalIndex, Qt::Horizontal).toString() : "";
    qDebug() << "text: " << text;
    geometry.setWidth(FPublicFunction::boundingRectOfTextAndFont(text, FPublicFunction::defaultNormalFont()).width());
    painter->setPen(QPen(Qt::white, 1));
    painter->setFont(FPublicFunction::defaultLargeFont());
    painter->drawText(geometry, text);

    // 绘制排序图标
    if (logicalIndex == this->sortIndicatorSection())
    {
        Qt::SortOrder order = this->sortIndicatorOrder();
        QString imageName = "ic_ascend";
        if (order == Qt::DescendingOrder) imageName = "ic_descend";

        geometry = QRect(geometry.right() + 4, 10, 10, 10);
        painter->drawImage(geometry, QImage(imageName));
    }
}

问题在于QAbstractTableModel::headerData()实现。
因为这个接口是用于提供Header展示内容(包括文字、图标、背景等等),如果只返回文案的内容,将会出错。

解决方案

更改QAbstractTableModel::headerData()实现方案为:

QVariant FFileTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        if (role == Qt::DisplayRole)
        {
            QString value;
            switch (section)
            {
            case 0: value = "a"; break;
            case 1: value = "b"; break;
            case 2: value = "c"; break;
            case 3: value = "d"; break;
            case 4: value = "e"; break;
            case 5: value = "f"; break;
            case 6: value = "g"; break;
            default: break;
            }
            return value;
        }
    }
    else if (orientation == Qt::Vertical)
    {
        return QVariant();
    }

    return QAbstractTableModel::headerData(section, orientation, role);
}

分别对orientation和role进行处理返回,以达到绘制文案、图标、背景等效果。而不是只返回文字内容。并且最后一句return也一定不能省略,否则也显示不出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值