qt之QCustomPlot动态更改曲线颜色,点击曲线标题名称

一、前言

上篇文章介绍了如何调用qt自带的颜色画板,其实目的就是想更改一些颜色,本篇博客介绍更改QCustomPlot的曲线颜色

二、环境

qt5.7 mingw 

windows8

sqlite3

三、正文

  1. 首先在数据库中建好表,一行多列,放置多个颜色值
  2. 在QCustomPlot初始化最后加上
        CustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom| QCP::iSelectAxes |
                                    QCP::iSelectLegend | QCP::iSelectPlottables);//可以进行鼠标位置 放大缩小 拖拽  放大缩小坐标系!!!功能非常强大
        CustomPlot->legend->setSelectableParts(QCPLegend::spItems);
        connect(CustomPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));

     点击曲线标题回调出颜色面板

    void MainWindow::selectionChanged()
    {
        for (int i=0; i<ui->widget1->graphCount(); ++i){
          QCPGraph *graph = ui->widget1->graph(i);
          QCPPlottableLegendItem *item = ui->widget1->legend->itemWithPlottable(graph);
          if (item->selected()/*||graph->selected()*/){
            QColorDialog *m_pColor = new QColorDialog(this);
            m_pColor->setWindowModality(Qt::ApplicationModal);
            m_pColor->setCurrentColor(QColor(Qt::red));//初始颜色
            m_pColor->show();
            m_pColor->move(720,200);
            connect(m_pColor,SIGNAL(currentColorChanged(QColor)),this,SLOT(ShowColor(QColor)));//显示当前选中颜色的效果
            connect(m_pColor,SIGNAL(colorSelected(QColor)),this,SLOT(SetColor(QColor)));//OK信号连接
          }
        }
    }

    颜色面板对应的确认拾取函数

  3. void MainWindow::ShowColor(const QColor &color)//只要当前颜色在对话框中发生改变,就会触发该信号。
    {
        qDebug()<<"1111"<<color;
    }
    void MainWindow::SetColor(const QColor &color)//当用户选中某一颜色并点击“OK”后,就会触发该信号。
    {
        QStringList hexStr;
        for(int i=0; i<ui->widget1->graphCount(); ++i){
          QCPGraph *graph = ui->widget1->graph(i);
          QCPPlottableLegendItem *item = ui->widget1->legend->itemWithPlottable(graph);
          if(item->selected()/*|| graph->selected()*/)
            graph->setPen(QPen(color));
    
          QString redStr = QString("%1").arg(graph->pen().color().red(),2,16,QChar('0'));
          QString greenStr = QString("%1").arg(graph->pen().color().green(),2,16,QChar('0'));
          QString blueStr = QString("%1").arg(graph->pen().color().blue(),2,16,QChar('0'));
          hexStr.append("#"+redStr+greenStr+blueStr);
        }
        QSqlTableModel *model = new QSqlTableModel(NULL, db);
        model->setTable("xxx");
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model->select();
        QSqlRecord record = model->record();
        record.setValue("graph1",hexStr.at(0));
        record.setValue("graph2",hexStr.at(1));
        record.setValue("graph3",hexStr.at(2));
        record.setValue("graph4",hexStr.at(3));
        model->setRecord(0, record);//修改的行 row_forsql
        model->submitAll();
    }

    到此就完成一大半了,拾取颜色改变颜色已经完成,最后还差初始化时将上次的颜色刷新到曲线中,用读取数据库的方式。在曲线初始化前获取颜色

  4.     QStringList colorlist;
        QSqlQuery qry(db);
        if(qry.exec("select * from xxx")&&qry.next()){
            for(int i=0;i<4;i++)colorlist.append(qry.value(i).toString());
        }

 

四、结语

大功告成

  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大桶矿泉水

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值