mainwindow.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4. #include <QMessageBox>
  5. #define WINDOW_SIZE 200
  6. MainWindow::MainWindow(QWidget *parent)
  7. : QMainWindow(parent)
  8. , ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11. initThread();
  12. initSiganl();
  13. initCombox();
  14. initPlot();
  15. }
  16. void MainWindow::initThread()
  17. {
  18. serialthread = new SerialThread();
  19. portTh = new QThread(this);
  20. serialthread->moveToThread(portTh);
  21. portTh->start();
  22. }
  23. void MainWindow::initSiganl()
  24. {
  25. connect(serialthread,&SerialThread::sigMessage,
  26. this,[=](QString msg)
  27. {
  28. QMessageBox::information(this,"错误",msg);
  29. });
  30. connect(this,&MainWindow::sigOpenSerial,serialthread,&SerialThread::openSerial,Qt::QueuedConnection);
  31. connect(serialthread,&SerialThread::sigTemperature,this,&MainWindow::updatePlot);
  32. }
  33. MainWindow::~MainWindow()
  34. {
  35. portTh->quit();
  36. portTh->wait();
  37. delete ui;
  38. }
  39. void MainWindow::initCombox()
  40. {
  41. QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
  42. if(ports.isEmpty())
  43. return;
  44. ui->comboBox_port->clear();
  45. for(const auto &info : ports)
  46. {
  47. ui->comboBox_port->addItem(info.portName());
  48. }
  49. }
  50. void MainWindow::initPlot()
  51. {
  52. for(int i = 1; i <= 32; i++)
  53. {
  54. m_names << QString::number(i);
  55. }
  56. QList<QColor> colors =
  57. {
  58. QColor("#E6194B"),QColor("#3CB44B"),QColor("#4363D8"),QColor("#F58231"),
  59. QColor("#911EB4"),QColor("#46F0F0"),QColor("#F032E6"),QColor("#BCF60C"),
  60. QColor("#FABEBE"),QColor("#008080"),QColor("#E6BEFF"),QColor("#9A6324"),
  61. QColor("#FFFAC8"),QColor("#800000"),QColor("#AAFFC3"),QColor("#808000"),
  62. QColor("#FFD8B1"),QColor("#000075"),QColor("#808080"),QColor("#A9A9A9"),
  63. QColor("#FF4500"),QColor("#2E8B57"),QColor("#1E90FF"),QColor("#DA70D6"),
  64. QColor("#B8860B"),QColor("#20B2AA"),QColor("#DC143C"),QColor("#7B68EE"),
  65. QColor("#228B22"),QColor("#FF1493"),QColor("#4682B4"),QColor("#CD853F")
  66. };
  67. for(int i = 0; i < m_names.size(); i++)
  68. {
  69. QCPGraph *graph = ui->customPlot->addGraph();
  70. graph->setName(m_names[i]); QPen pen(colors[i]);
  71. pen.setWidth(1);
  72. graph->setPen(pen);
  73. }
  74. ui->customPlot->xAxis->setLabel("Time");
  75. ui->customPlot->yAxis->setLabel("Temperature (°C)");
  76. ui->customPlot->xAxis->setRange(0, WINDOW_SIZE);
  77. ui->customPlot->yAxis->setRange(0.0, 100.0);
  78. ui->customPlot->yAxis->setNumberFormat("f");
  79. ui->customPlot->yAxis->setNumberPrecision(1);
  80. // 主刻度: 每5℃显示数字
  81. QSharedPointer<QCPAxisTickerFixed> yTicker(new QCPAxisTickerFixed);
  82. yTicker->setTickStep(5);
  83. ui->customPlot->yAxis->setTicker(yTicker);
  84. // 开启子刻度
  85. ui->customPlot->yAxis->setSubTicks(true);
  86. // X轴不要网格(不要竖线)
  87. ui->customPlot->xAxis->grid()->setVisible(false);
  88. // X轴不要子刻度
  89. ui->customPlot->xAxis->setSubTicks(false);
  90. // Y轴主网格
  91. ui->customPlot->yAxis->grid()->setVisible(true);
  92. // Y轴子网格
  93. ui->customPlot->yAxis->grid()->setSubGridVisible(true);
  94. // ================= 允许x轴拖动 缩放 =================
  95. ui->customPlot->setInteractions(QCP::iNone);
  96. ui->customPlot->replot();
  97. }
  98. void MainWindow::on_connect_btn_clicked()
  99. {
  100. QString portname = ui->comboBox_port->currentText();
  101. emit sigOpenSerial(portname);
  102. }
  103. void MainWindow::updatePlot(QVector<double> values)
  104. {
  105. for(int i = 0; i < 32; i++)
  106. {
  107. if(!std::isnan(values[i]))
  108. {
  109. ui->customPlot->graph(i)->addData(m_timeIndex,values[i]);
  110. }
  111. }
  112. for(int i=0;i<32;i++)
  113. {
  114. ui->customPlot->graph(i)->data()->removeBefore(m_timeIndex - WINDOW_SIZE);
  115. }
  116. if(m_timeIndex > WINDOW_SIZE)
  117. {
  118. ui->customPlot->xAxis->setRange(m_timeIndex - WINDOW_SIZE,m_timeIndex);
  119. }
  120. ui->customPlot->replot();
  121. m_timeIndex++;
  122. }