【Qt自定义QSqlTableModel实现表格设置单元格字体颜色】

本文介绍了如何在Qt中使用QSqlTableModel修改单元格字体颜色,并展示了如何自定义表头以添加复选框。作者分享了自定义MySubClassedSqlTableModel类和customtableheaderview的实现,以实现在QTableView中添加自定义功能。
摘要由CSDN通过智能技术生成


一、前言

记录下Qt使用QTableView设置QSqlTableModel如何修改单元格字体颜色,以及自定义表头实现添加自己想要的按钮等自定义控件,都是参考网上自己改成自己项目需要的,希望也能对你们有点参考。


提示:以下是本篇文章正文内容,下面案例可供参考

二、效果展示


请添加图片描述

三、自定义MySubClassedSqlTableModel

代码如下(示例):

1、MySubClassedSqlTableModel

#include <QSqlTableModel>
class MySubClassedSqlTableModel : public QSqlTableModel
{
    Q_OBJECT
public:
    MySubClassedSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlTableModel(parent,db) {;}
    QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
    {
        if(role==Qt::TextColorRole)
        {
            const QVariant value(data(index,Qt::DisplayRole));
            if(index.column() == 3){
                if(value.toString() == "在线") return QVariant(QColor(4,217,25));
                else return QVariant(QColor(217,0,27));
            }
            else if(index.column() == 5){
                if(value.toString() == "NO") return QVariant(QColor(245,154,35));
                else return QVariant(QColor(255,255,255));
            }
            else if(index.column() == 6){
                if(value.toString() == "0@0") return QVariant(QColor(245,154,35));
                else return QVariant(QColor(255,255,255));
            }
            else if(index.column() == 7){
                if(value.toString() == "已连接") return QVariant(QColor(4,217,25));
                else return QVariant(QColor(255,255,255));
            }
            else if(index.column() == 8){
                if(value.toString() == "YES") return QVariant(QColor(4,217,25));
                else if(value.toString() == "未知") return QVariant(QColor(245,154,35));
                else return QVariant(QColor(255,255,255));
            }
            else if(index.column() == 9){
                if(value.toString() == "未连接" || value.toString() == "不涉及") return QVariant(QColor(255,255,255));
                else return QVariant(QColor(4,217,25));
            }
            else if(index.column() == 10){
                double usage = value.toString().replace("%", "").toDouble();
                if(usage >= 90) return QVariant(QColor(217,0,27));
                else if(usage >= 75 && usage <= 90) return QVariant(QColor(245,154,35));
                else return QVariant(QColor(255,255,255));
            }
            else if(index.column() == 11){
                double usage = value.toString().replace("%", "").toDouble();
                if(usage >= 90) return QVariant(QColor(217,0,27));
                else if(usage >= 75 && usage <= 90) return QVariant(QColor(245,154,35));
                else return QVariant(QColor(255,255,255));
            }
            else{
                return QVariant(QColor(255,255,255));
            }
        }
        return QSqlTableModel::data(index,role);
    }
};



2、使用

代码如下(示例):

//创建一个QpSqlTableModel模型
MySubClassedSqlTableModel *pSqlTableModel = new MySubClassedSqlTableModel(this,db);
//设置模型表名
pSqlTableModel->setTable("HistoryTable");
pSqlTableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
//设置表头名称
QStringList headerDataList;
headerDataList << tr("全部") << tr("归属分组") << tr("名称") << tr("状态") << tr("IP") << tr("HDMI IN") << tr("输入分辨率") << tr("USB接主机")
                   << tr("HDMI OUT") << tr("USB接外设") << tr("CPU利用率") << tr("内存利用率") << tr("网速输入") << tr("网速输出") << tr("运行时长")
                   << tr("入网时间") << tr("采集时间") << tr("uuid");
for (int i = 0; i < headerDataList.count(); ++i) {
	pSqlTableModel->setHeaderData(i, Qt::Orientation::Horizontal, headerDataList.at(i));
}
pSqlTableModel->select();
//显示到Qtableview控件上
this->setModel(pSqlTableModel);

四、自定义表头实现表头添加自定义复选框

1、customtableheaderview.h

代码如下(示例):

#ifndef CUSTOMTABLEHEADERVIEW_H
#define CUSTOMTABLEHEADERVIEW_H

#include <QStyledItemDelegate>
#include <QHeaderView>
#include <QMouseEvent>
#include <QPainter>
#include <QCheckBox>

class QtableviewDelegate : public QStyledItemDelegate
{
public:
    QtableviewDelegate();
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

class CustomTableHeaderView : public QHeaderView
{
    Q_OBJECT
public:
    CustomTableHeaderView(Qt::Orientation orientation, QWidget * parent = 0);
    inline QCheckBox *getCheckBox(){return m_checkBox;}
public:
    // 设置自定义ComboBox;
    void setComboBoxObject(QCheckBox* object);
protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
Q_SIGNALS:
    void currentSelectedColumnChange(int col);
private:
    void mousePressEvent(QMouseEvent * event);      //鼠标点击事件
private:
    QCheckBox * m_checkBox;
};

#endif // CUSTOMTABLEHEADERVIEW_H

2、customtableheaderview.cpp

代码如下(示例):

#include "customtableheaderview.h"

QtableviewDelegate::QtableviewDelegate()
{
    
}

void QtableviewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem new_option(option);
    new_option.displayAlignment = Qt::AlignCenter;
    if (new_option.state & QStyle::State_Selected) {
        if(index.column() != 0) new_option.font.setBold(true);
    }
    QStyledItemDelegate::paint(painter, new_option, index);
}

CustomTableHeaderView::CustomTableHeaderView(Qt::Orientation orientation, QWidget *parent)
    :QHeaderView(orientation, parent)
{
    m_checkBox = new QCheckBox(this);
    m_checkBox->setFixedSize(25,25);
}

void CustomTableHeaderView::setComboBoxObject(QCheckBox *object)
{
    m_checkBox = object;
    m_checkBox->setParent(this);
}

void CustomTableHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    if (logicalIndex == 0) m_checkBox->move(0 ,(rect.height()-m_checkBox->height())/2);
    QHeaderView::paintSection(painter, rect, logicalIndex);
}

void CustomTableHeaderView::mousePressEvent(QMouseEvent *event)
{
    if((event->button() == Qt::LeftButton)) {
        emit currentSelectedColumnChange(logicalIndexAt(event->pos()));
    }
}


3、自定义表头使用

代码如下(示例):

QtableviewDelegate * delegate = new QtableviewDelegate;
this->setItemDelegate(delegate);
CustomTableHeaderView *m_customTableHeaderView = new CustomTableHeaderView(Qt::Horizontal, this);
this->setHorizontalHeader(m_customTableHeaderView);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值