#include "realTimeDataDisplay.h" realTimeDataDisplay::realTimeDataDisplay(QWidget* parent) : QMainWindow(parent) { ui.setupUi(this); mPlot = new QCustomPlot(this); setCentralWidget(mPlot); //mPlot->yAxis->setTickLabels(false); mPlot->axisRect()->axis(QCPAxis::atRight, 0)->setPadding(54); mGraph1 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0)); QColor color1(100, 140, 180); QPen pen; pen.setStyle(Qt::PenStyle::SolidLine); pen.setColor(color1); mGraph1->setPen(pen); color1.setAlpha(100); mPlot->graph(0)->setBrush(QBrush(color1)); mPlot->graph(0)->setName("sin(t)+sin(t/0.38)"); mGraph2 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0)); QColor color2(166, 77, 157); pen.setStyle(Qt::PenStyle::SolidLine); pen.setColor(color2); mGraph2->setPen(pen); color2.setAlpha(100); mPlot->graph(1)->setBrush(QBrush(color2)); mPlot->graph(1)->setName("sin(t)"); QSharedPointer timeTicker(new QCPAxisTickerTime); timeTicker->setTimeFormat("%h:%m:%s"); mPlot->xAxis->setTicker(timeTicker); mPlot->xAxis->setRange(0, 16); mPlot->yAxis->setRange(-2.2, 2.2); mPlot->xAxis->setLabel("time"); mPlot->yAxis->setLabel("value"); mPlot->plotLayout()->insertRow(0); QCPTextElement* title = new QCPTextElement(mPlot, "Realtime Data Display"); mPlot->plotLayout()->addElement(0, 0, title); connect(mPlot, &QCustomPlot::mousePress, this, [=] { xAxisAutoChange = false; }); connect(mPlot, &QCustomPlot::mouseRelease, this, [=] { xAxisAutoChange = true; }); mPlot->setInteractions(QCP::iRangeDrag); QListlist; list.append(mPlot->xAxis); mPlot->axisRect()->setRangeDragAxes(list); mPlot->axisRect()->setupFullAxesBox(); mTimerThread = new QThread(this); mDataTimer = new QTimer(); connect(mDataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()), Qt::DirectConnection); connect(mTimerThread, &QThread::finished, mDataTimer, &QObject::deleteLater); connect(mTimerThread, &QThread::finished, mTimerThread, &QObject::deleteLater); connect(this, &realTimeDataDisplay::updateUICustomPlot, this, &realTimeDataDisplay::onUpdateUICustomPlot); mTimerThread->start(); mDataTimer->start(16); mDataTimer->moveToThread(mTimerThread); qDebug() << "this Thread:" << QThread::currentThreadId() << QThread::currentThread(); mTag1 = new AxisTag(mGraph1->valueAxis()); mTag1->setPen(mGraph1->pen()); mTag2 = new AxisTag(mGraph2->valueAxis()); mTag2->setPen(mGraph2->pen()); mPlot->legend->setVisible(true); mPlot->legend->setBrush(QColor(255, 255, 255, 150)); } realTimeDataDisplay::~realTimeDataDisplay() { if (mDataTimer) { mTimerThread->quit(); mTimerThread->wait(); //connect: &QObject::deleteLater,不需要delete mTimerThread = nullptr; mDataTimer = nullptr; } } void realTimeDataDisplay::onUpdateUICustomPlot(double key, double v1, double v2) { if (xAxisAutoChange) mPlot->xAxis->setRange(key, 16, Qt::AlignRight); mTag1->updatePosition(v1); mTag2->updatePosition(v2); if (v1 > 0) mTag1->setText("+" + QString::number(v1, 'f', 2)); else mTag1->setText(QString::number(v1, 'f', 2)); if (v2 > 0) mTag2->setText("+" + QString::number(v2, 'f', 2)); else mTag2->setText(QString::number(v2, 'f', 2)); mPlot->replot(); } void realTimeDataDisplay::timerSlot() { //qDebug() << "timerSlot Thread:" << QThread::currentThreadId() << QThread::currentThread(); static QTime timeStart = QTime::currentTime(); double key = timeStart.msecsTo(QTime::currentTime()) / 1000.0; static double lastFpsKey; static int frameCount; ++frameCount; if (key - lastFpsKey > 2) // average fps over 2 seconds { ui.statusBar->showMessage(QString("%1 FPS").arg(frameCount / (key - lastFpsKey), 0, 'f', 0), 0); lastFpsKey = key; frameCount = 0; } mPlot->graph(0)->addData(key, qSin(key) + qSin(key / 0.3843)); mPlot->graph(1)->addData(key, qSin(key)); double graph1Value = mGraph1->dataMainValue(mGraph1->dataCount() - 1); double graph2Value = mGraph2->dataMainValue(mGraph2->dataCount() - 1); emit updateUICustomPlot(key, graph1Value, graph2Value); //mTag1->updatePosition(graph1Value); //mTag2->updatePosition(graph2Value); //if (graph1Value > 0) // mTag1->setText("+" + QString::number(graph1Value, 'f', 2)); //else // mTag1->setText(QString::number(graph1Value, 'f', 2)); //if (graph2Value > 0) // mTag2->setText("+" + QString::number(graph2Value, 'f', 2)); //else // mTag2->setText(QString::number(graph2Value, 'f', 2)); //mPlot->replot(); }