代码是主函数,不仅仅使用了switchbutton 还使用了绘图十分方便的Qcustomplot界面的话效果是:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "switchbutton.h"
#include "qcustomplot.h"
#include <iostream>
#include <QDebug>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// SwitchButton * button= new SwitchButton(this);
timer = new QTimer(this);
timer->setInterval(20);
connect(timer,&QTimer::timeout,this,&MainWindow::timeChartInit);
connect(ui->sb1,&SwitchButton::Clicked,this,&MainWindow::DebugFuction);
connect(ui->sb2,&SwitchButton::Clicked,this,&MainWindow::DebugFuction2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::DebugFuction(bool flage)
{
if(flage)
{
customplotInit();
timer->start();
}
else
{
ui->customplot->clearGraphs();
timer->stop();
}
ui->customplot->replot();
}
void MainWindow::DebugFuction2(bool flage)
{
if(flage)
{
timer->start();
}
else
timer->stop();
}
void MainWindow::customplotInit()
{
// xporint.resize(100);
// yporint.resize(100);
// for (int i = 0; i < 100; ++i) {
// xporint[i] = i; // -1 到 1
// yporint[i] = qrand()%100;
// }
// 自动扩展
ui->customplot->rescaleAxes();
ui->customplot->replot(QCustomPlot::rpQueuedReplot);
ui->customplot->setOpenGl(false);
ui->customplot->addGraph();
ui->customplot->addGraph();
// 可滚动可缩放
ui->customplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
// 设置曲线画笔
// ui->customplot->graph(0)->setLineStyle((QCPGraph::lsStepLeft));
// ui->customplot->graph(1)->setLineStyle((QCPGraph::lsStepLeft));
// 设置曲线上点的风格(样式)
// ui->customplot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
// 设置曲线风格
ui->customplot->graph(0)->setPen(QPen(Qt::red));
ui->customplot->graph(0)->setBrush(QBrush(QColor(255,0,0,20)));
ui->customplot->graph(1)->setPen(QPen(Qt::green));
ui->customplot->graph(1)->setBrush(QBrush(QColor(122,10,144,20)));
// //设置曲线形状
// QCPGraph::setBrush(const QBrush &brush);
// //设置与某之间曲线填充
// QCPGraph::setChannelFillGraph(otherGraph);
// //移除填充
// QCPGraph::setBrush(Qt::NoBrush);
// ui->customplot->graph(0)->setData(xporint, yporint);
// ui->customplot->graph(0)->setName("TEST");
// ui->customplot->xAxis->setLabel("x");
// ui->customplot->yAxis->setLabel("y");
// ui->customplot->xAxis->setRange(0, 100);
// ui->customplot->yAxis->setRange(0, 100);
// ui->customplot->legend->setVisible(true);
ui->customplot->replot();
}
void MainWindow::timeChartInit()
{
if(xporint.size()<100)
{
xporint.append(xporint.size());
yporint.append(qrand()%100);
xporint1.append(xporint.size());
yporint1.append(qrand()%100);
}
else
{
yporint.remove(0);
yporint.append(qrand()%100);
yporint1.remove(0);
yporint1.append(qrand()%100);
}
ui->customplot->graph(0)->setData(xporint, yporint);
ui->customplot->graph(0)->setName("b1");
ui->customplot->graph(1)->setData(xporint1, yporint1);
ui->customplot->graph(1)->setName("b3");
ui->customplot->xAxis->setLabel("x");
ui->customplot->yAxis->setLabel("y");
ui->customplot->xAxis->setRange(0, 100);
ui->customplot->yAxis->setRange(0, 100);
ui->customplot->legend->setVisible(true);
ui->customplot->replot();
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
// 获取鼠标点击的像素坐标
QPoint clickPos = event->pos();
// 将像素坐标转换为坐标轴数值
QPainter painter(this);
painter.setPen(Qt::blue);
painter.setBrush(Qt::yellow);
QLineF line(clickPos.x(),clickPos.y(),ui->widget->width(),clickPos.y());
painter.drawLine(line);
}
}