曲線監控模塊用的不多,主要就是用來觀察某個設備的實時採集的數據和歷史採集的數據,能夠回放數據,在右側能夠選擇對應的通訊端口和控制器,而後選擇指定的探測器進行觀察,從選擇的時候開始計時,每一個數據都對應一個數據點,至於採集間隔,這個在端口管理中設定的,通常來講都是1秒鐘採集一次。node
顯示曲線圖表控件,我的強烈推薦開源的qcustomplot或者qwt,Qt5.7之後集成了qchart模塊也有曲線控件,我的以爲用法仍是不夠友好,並且不少人反映大數據量基本上歇菜,只能作一些小數據量的展現,我看過qchart模塊的完整源碼,寫的不是很好,也許後期改進了很多,仍是Qt4時代若基亞品質保證,代碼質量都是槓槓的,惋惜若基亞把Qt賣了。mysql
皮膚開源:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo 文件名稱:styledemolinux
體驗地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe 文件名稱:bin_sams.zipc++
void frmViewPlot::initPlot() { ui->customPlot->addGraph(); ui->customPlot->graph(0)->setName("濃度值"); ui->customPlot->graph(0)->setPen(QPen(QColor(App::ColorPlotLine), LineWidth)); ui->customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::NoPen, QBrush(QColor(App::ColorPlotLine)), DotWidth)); QFont font; font.setPixelSize(11); ui->customPlot->legend->setFont(font); ui->customPlot->xAxis->setLabelFont(font); ui->customPlot->yAxis->setLabelFont(font); ui->customPlot->xAxis->setTickLabelFont(font); ui->customPlot->yAxis->setTickLabelFont(font); ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime); ui->customPlot->xAxis->setDateTimeFormat("hh:mm:ss"); ui->customPlot->xAxis->setAutoTickStep(false); ui->customPlot->xAxis->setTickStep(TickStep); ui->customPlot->xAxis->setRange(0, XMax, Qt::AlignRight); ui->customPlot->yAxis->setRange(0, 100); ui->customPlot->legend->setVisible(true); QColor textColor = QColor(App::ColorPlotText); ui->customPlot->yAxis->setLabelColor(textColor); ui->customPlot->xAxis->setTickLabelColor(textColor); ui->customPlot->yAxis->setTickLabelColor(textColor); ui->customPlot->xAxis->setBasePen(QPen(textColor)); ui->customPlot->yAxis->setBasePen(QPen(textColor)); ui->customPlot->xAxis->setTickPen(QPen(textColor)); ui->customPlot->yAxis->setTickPen(QPen(textColor)); ui->customPlot->xAxis->setSubTickPen(QPen(textColor)); ui->customPlot->yAxis->setSubTickPen(QPen(textColor)); ui->customPlot->xAxis->grid()->setPen(QPen(textColor)); ui->customPlot->yAxis->grid()->setPen(QPen(textColor)); ui->customPlot->yAxis->grid()->setZeroLinePen(QPen(textColor)); ui->customPlot->setBackground(QColor(App::ColorPlotBg)); //ui->customPlot->xAxis->setTickLabelRotation(90); ui->customPlot->yAxis->setAutoTickCount(30); ui->customPlot->replot(); //ui->customPlot->installEventFilter(this); } void frmViewPlot::loadPlot() { //移除第一個數據,增長一個數據,保證每次集合數據一致 labs.remove(0, 1); labs.append(TIME); values.remove(0, 1); values.append(currentValue); ui->customPlot->graph(0)->setData(keys, values); ui->customPlot->xAxis->setTickVector(keys); ui->customPlot->xAxis->setTickVectorLabels(labs); ui->customPlot->replot(); } void frmViewPlot::initData() { whereSql = "where 1=1"; columnNames << "編號" << "位號" << "控制器名稱" << "探測器名稱" << "濃度值" << "氣體符號" << "保存時間"; columnWidths << 100 << 120 << 145 << 145 << 70 << 70 << 150; //設置須要顯示數據的表格和翻頁的按鈕,最後一列自動填充,奇偶行不一樣顏色顯示 dbPage = new DbPage(this); dbPage->setAllCenter(true); dbPage->setColumnNames(columnNames); dbPage->setColumnWidths(columnWidths); dbPage->setResultCurrent(XMax2); dbPage->setTableName("NodeLog"); dbPage->setOrderSql(QString("LogID %1").arg(App::AlarmLogOrder)); dbPage->setControl(ui->tableView, ui->labPageCount, ui->labPageCurrent, ui->labResultCount, ui->labResultCurrent, ui->labResult, 0, ui->btnFirst, ui->btnPre, ui->btnNext, ui->btnLast, "LogID"); dbPage->setWhereSql(whereSql); dbPage->select(); //綁定按鈕切換載入數據 connect(ui->btnFirst, SIGNAL(clicked()), this, SLOT(loadData())); connect(ui->btnPre, SIGNAL(clicked()), this, SLOT(loadData())); connect(ui->btnNext, SIGNAL(clicked()), this, SLOT(loadData())); connect(ui->btnLast, SIGNAL(clicked()), this, SLOT(loadData())); } void frmViewPlot::loadData() { keys.clear(); labs.clear(); values.clear(); //加載當前表格中的數據 QAbstractItemModel *model = ui->tableView->model(); int count = model->rowCount(); for (int i = 0; i < count; i++) { QModelIndex modelIndex = model->index(i, 4); QModelIndex labIndex = model->index(i, 6); double value = model->data(modelIndex).toDouble(); QString strDate = model->data(labIndex).toString().right(8); keys << i; labs << strDate; values << value; } ui->customPlot->graph(0)->setData(keys, values); ui->customPlot->xAxis->setTickVector(keys); ui->customPlot->xAxis->setTickVectorLabels(labs); ui->customPlot->replot(); } void frmViewPlot::receiveValue(const QString &positionID, float value) { if (ui->ckPause->isChecked()) { return; } //必須是實時曲線才須要顯示 if (ui->cboxType->currentText() != "實時曲線") { return; } //找到當前位號對應的索引,取出對應索引位置的值 QString id = ui->cboxNodeName->itemData(ui->cboxNodeName->currentIndex()).toString(); if (id != positionID) { return; } currentValue = value; //如下增長部分爲將接收數據曲線顯示 QVector<double> keys; QVector<double> datas; double key = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0; keys.append(key); datas.append(currentValue); ui->customPlot->graph(0)->addData(keys, datas); ui->customPlot->graph(0)->removeDataBefore(key - XMax); ui->customPlot->xAxis->setRange(key, XMax, Qt::AlignRight); ui->customPlot->replot(); } void frmViewPlot::receiveValue(const QString &portName, quint8 addr, const QList<float> &values) { if (ui->ckPause->isChecked()) { return; } //必須是實時曲線才須要顯示 if (ui->cboxType->currentText() != "實時曲線") { return; } //過濾端口 if (ui->cboxPortName->currentText() != portName) { return; } //過濾設備 if (ui->cboxDeviceName->itemData(ui->cboxDeviceName->currentIndex()).toInt() != addr) { return; } //找到當前位號對應的索引,取出對應索引位置的值 QString positionID = ui->cboxNodeName->itemData(ui->cboxNodeName->currentIndex()).toString(); //找到當前索引位置的設備地址對應探測器的最小寄存器地址 //若是讀取的起始寄存器地址是5則回來的數據位第一個是寄存器地址5的數據,後面連續 quint16 nodeMinAddr = DBHelper::getNodeMinAddr(portName, addr); int index = DBData::NodeInfo_PositionID.indexOf(positionID); int startIndex = DBData::NodeInfo_NodeAddr.at(index) - nodeMinAddr - 1; //有時候可能出現總共添加了8個探測器可是真實讀到4個探測器的狀況 if (startIndex >= values.count()) { return; } currentValue = values.at(startIndex); //如下增長部分爲將接收數據曲線顯示 QVector<double> keys; QVector<double> datas; double key = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0; keys.append(key); datas.append(currentValue); ui->customPlot->graph(0)->addData(keys, datas); ui->customPlot->graph(0)->removeDataBefore(key - XMax); ui->customPlot->xAxis->setRange(key, XMax, Qt::AlignRight); ui->customPlot->replot(); }