文章目录
一、前言
记录下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);