QT提取数据库内容,根据不同字段要求显示不同颜色

1、使用mysql查询数据库内容,并且显示在tabview中,

 					 //插入数据
            QString queryStr = QString("select * from phm_table  limit 5");
            Sqlmodel = new QSqlQueryModel();
            Sqlmodel->setQuery(queryStr);
            qDebug() << "ok";
            ui->tableView->setModel(Sqlmodel);

需求:通过点击 详情进入指定界面。

在这里插入图片描述
实现:右击转到槽。
在这里插入图片描述
在槽函数中添加如下代码:
获取当前的行号和列号,然后调用指定槽函数实现响应的功能。

void MainWindow::on_tableView_clicked(const QModelIndex &index)
{

    if(index.row() == 0&&index.column()==5)qDebug()<<"123";

    if(index.row() == 1&&index.column()==5)qDebug()<<"456";

}

进阶版

按下第一行或者第二行第五列元素,打印对应行的第一列元素

void MainWindow::on_tableView_clicked(const QModelIndex &index)
{

    QModelIndex pindex;
    QString pstr ;
    if(index.row() == 0&&index.column()==5){
        pindex = Sqlmodel->index(0,0);
        pstr = Sqlmodel->data(pindex).toString();
        qDebug()<<"data is: +"<<pstr;
    }
    if(index.row() == 1&&index.column()==5){
        pindex = Sqlmodel->index(1,0);
        pstr = Sqlmodel->data(pindex).toString();
        qDebug()<<"data is: +"<<pstr;
    }

}

在这里插入图片描述

扩展

获取某一行一列的数据


void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
    QModelIndex pindex;
    QString pstr ;
    for(int i=0;i<Sqlmodel->columnCount();i++)
    {
        pindex = Sqlmodel->index(index.row(),i);
        pstr = Sqlmodel->data(pindex).toString();
        qDebug()<<"data is: +"<<pstr;
    }
}

当按下一行时,显示当前行的数据信息。结果展示:
在这里插入图片描述

打印某一行的数据:
在这里插入图片描述
2022.8.30完成指定数据库颜色显示:

遇到贵人了,远程帮我解决难题。

最最最重要的细节核心代码,使用完成颜色改变显示。

#ifndef TABLEITEMDELEGATE_H
#define TABLEITEMDELEGATE_H

#include <QItemDelegate>
#include <QPainter>
#include <QPushButton>

class  TableItemDelegate : public  QItemDelegate
{
    Q_OBJECT
public:
    TableItemDelegate(QObject *parent = 0) : QItemDelegate(parent) { }


    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index)const
    {
        //画背景
        QString colorBK = "#2564C8";
        //		if (option.state & QStyle::State_Selected)
        //		{
        //            colorBK = "#FFFF00";    //选中状态的背景色 黄色
        //		}
        //        else if (option.state & QStyle::State_MouseOver)
        //        {
        //            colorBK = "#0000FF";    //鼠标移上去的背景色
        //        }
        painter->setPen(Qt::NoPen);
        painter->setBrush(QColor(colorBK));
        painter->drawRect(option.rect);

        //画文字
        QString colorText = "#FFFFFF";
        //        if(index.column() == 2)
        //        {
        //            colorText = "#FF0000";
        //        }
        //        if(index.column() == 3)
        //        {
        //            colorText = "#00FF00";
        //        }

        QString text = index.model()->data(index, Qt::DisplayRole).toString();
        if(text == "报警"){
            colorText = "#FF0000";
            colorBK = "#FFFF00";    //选中状态的背景色 黄色
        }

        else if(text == "预警"){
            colorText = "#0000FF";

        }

        else if(text == "离线"){
            colorText = "#FFFF00";
            colorBK = "#FFFF00";    //选中状态的背景色 黄色
        }

        else if(text == "健康"){
             colorBK = "#FFFF00";    //选中状态的背景色 黄色
             colorText = "#00CD66";
        }

        painter->setPen(colorText);
        painter->setBrush(QBrush(Qt::NoBrush));
        painter->drawText(option.rect, Qt::AlignCenter, text);
    }
};

#endif 

显示部分代码:

    model->setQuery(queryStr);
    Sqlmodel = new QSqlQueryModel();
    Sqlmodel->setQuery(queryStr);
    ui->tableView->setModel(model);
    ui->tableView->setItemDelegate(new TableItemDelegate(this));*

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值