• 1164阅读
  • 0回复

QStyledItemDelegate代理疑问 [复制链接]

上一主题 下一主题
离线lwei24
 

只看楼主 倒序阅读 楼主  发表于: 2021-02-20

各位大佬,请问
如下代码,自己写的代理,如何在第二列中插入文件名和该文件的默认图标呢?

void myItemDelegate2All::paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const
{
    QStyleOptionViewItem viewOption(option);

    viewOption.state &= ~QStyle::State_HasFocus;
    myTableView2 *view = qobject_cast<myTableView2 *>(viewOption.styleObject);
    QTableView::SelectionBehavior behavior = view->selectionBehavior();
    QModelIndex hoverIndex = view->hoverIndex();

    if (!(option.state & QStyle::State_Selected) && behavior != QTableView::SelectItems)
    {
        if (behavior == QTableView::SelectRows && hoverIndex.row() == index.row())
            viewOption.state |= QStyle::State_MouseOver;
        if (behavior == QTableView::SelectColumns && hoverIndex.column() == index.column())
            viewOption.state |= QStyle::State_MouseOver;
    }

    if (viewOption.state & QStyle::State_HasFocus)
    {
            viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
    }

    if(viewOption.state & QStyle::State_MouseOver)
        painter->fillRect(viewOption.rect, QColor(180,200,220));

    QStyledItemDelegate::paint(painter, option, index);

    if(index.isValid())
    {
        painter->save();
        int column = index.column();
        QVariant v = index.data(column + (Qt::UserRole+1)); //获取数据
        if(0 == column)
        {
            int id = v.toInt();
            painter->drawText(option.rect, Qt::AlignCenter, QString::number(id));

        }
        else if(1 == column)
        {
            int status = v.toInt();
            if(status == 1)
            {
                painter->drawPixmap(option.rect, m_lockedPng);
            }
            else if(status == 0)
            {
                painter->drawPixmap(option.rect, m_unlockedPng);
            }
            else
            {
                painter->drawText(option.rect, Qt::AlignCenter, "");
            }
        }
        else if(2 == column)
        {
            //此处如何通过自定义的数据模型,将获取出来的QIcon文件默认图标画出来
           //原本想QIcon icon = v.to--->这个怎么获取出QIcon的图标出来呢?

           //文件名
            QString fileName = v.toString();
            painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, fileName);
        }
        else if(3 == column)
        {
            QString dateTime = v.toString();
            painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter,dateTime);
        }
        else if(4 == column)
        {
            QString strType = v.toString();
            painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter,strType);
        }
        else if(5 == column)
        {
            QString strSize;
            if(v.toString() == "")
                strSize = "";
             else
                strSize = QString("%1 kb").arg(v.toString());
            painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, strSize);
        }
        painter->restore();
    }
}

重写了数据模型,继承于QAbstractTableModel。在重写下面两个方法时,怎么在第二行将文件的默认图标和文件名一起放在呢?
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value,int role = Qt::EditRole) override;


具体代码如下:
bool myTableModel2::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    int nColumn = index.column();
    FileRecord record = m_recordList.at(index.row());
    switch (role)
    {
    case Qt::TextAlignmentRole:
    {
        return Qt::AlignLeft | Qt::AlignHCenter;
    }
    case Qt::DisplayRole:
    {
        if(nColumn == FILE_IDNO_COLUMN)
        {
            record.id = value.toInt();
            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }

        if (nColumn == LOCK_ICON_COLUMN)
        {
            record.isLocked = value.toInt();
            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }

        if (nColumn == FILE_NAME_COLUMN)
        {
            record.fileName = value.toString(); /./此处怎么将文件的默认图标和文件文件名一起放在第二列,就像windows资源管理器显示的一样,左边是默认图标,紧接着是文件名。
/./好像没有toIcon这种转换,要怎么实现呢?
            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }

        if (nColumn == DATE_TIME_COLUMN)
        {
            record.dateTime = value.toString();
            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }

        if (nColumn == FILE_TYPE_COLUMN)
        {
            record.fileType = value.toString();
            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }

        if (nColumn == FILE_SIZE_COLUMN)
        {
            record.fileSize = value.toString();
            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }

    }
    default:
        return false;
    }
    return false;
}

QVariant myTableModel2::data(const QModelIndex &index, int role) const
{

    QVariant result;
    switch (role - (Qt::UserRole + 1))
    {
    case 0:
        result = QVariant(m_recordList[index.row()].id);
        break;
    case 1:
        result = QVariant(m_recordList[index.row()].isLocked);
        break;
    case 2:
        result = QVariant(m_recordList[index.row()].fileName); /./此处怎么将文件的默认图标和文件文件名一起放在第二列,就像windows资源管理器显示的一样,左边是默认图标,紧接着是文件名。
        break;
    case 3:
        result = QVariant(m_recordList[index.row()].dateTime);
        break;
    case 4:
        result = QVariant(m_recordList[index.row()].fileType);
        break;
    case 5:
        result = QVariant(m_recordList[index.row()].fileSize);
        break;
    }

    return result;
}


求助各位大佬,在线等。希望能够解决这个问题,不胜感谢!!!!







快速回复
限100 字节
 
上一个 下一个