【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也一定不能省略,否则也显示不出

你可能感兴趣的:(qt,开发语言)