加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何在QTableView中为特定单元格着色或使文本变为粗体?

发布时间:2020-12-16 06:46:10 所属栏目:百科 来源:网络整理
导读:我已经使用QTableView来查看我的Qt程序中的表格数据,并且我需要将某些单元格区别于其他单元格,可以在这些特定单元格中绘制字体粗体或绘制这些特定单元格的背景. 有人可以提供代码,而不仅仅是说使用QAbstractItemDelegate吗? 我阅读了QAbstractItemDelegate
我已经使用QTableView来查看我的Qt程序中的表格数据,并且我需要将某些单元格区别于其他单元格,可以在这些特定单元格中绘制字体粗体或绘制这些特定单元格的背景.

有人可以提供代码,而不仅仅是说使用QAbstractItemDelegate吗?

我阅读了QAbstractItemDelegate的文档,但无法理解,请使用示例进行解释.

解决方法

无需使用抽象委托. Styled delegate完成了您需要的大部分工作.使用它并重新实现只需要的行为.

.H:

#include <QStyledItemDelegate>

class MyDelegate : public QStyledItemDelegate
{
    Q_OBJECT

    public:
        explicit MyDelegate(QObject *parent = 0);

        void paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const;

    private:
        bool shouldBeBold(const QModelIndex &index);
}

的.cpp:

MyDelegate::MyDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}


void MyDelegate::paint(QPainter *painter,const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt,index);

    QVariant data = index.data(...); // pick the data you need here
    opt.font.setBold(shouldBeBold(data));

    QStyledItemDelegate::paint(painter,opt,index);
}

bool MyDelegate::shouldBeBold(const QModelIndex &index)
{
    // you need to implement this
}

然后将委托应用于视图.如果shouldBeBold()返回false,则委托将绘制为标准的.如果返回true,则将应用粗体字体.

我希望你能够开始.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读