[英]QCustomPlot - Plots in various tabs
我目前正在通過串行通訊接收信息,請參閱下面的數據示例:
"A Ch1:45.23 Ch2:23.58 Ch3:12.45 Ch4:1.56"
"B Ch1:12.63 Ch2:15.45 Ch3:6.23 Ch4:45.32"
"C Ch1:22.20 Ch2:3.85 Ch3:2.45 Ch4:51.58"
"D Ch1:21.25 Ch2:2.58 Ch3:12.13 Ch4:61.52"
"A Ch1:4.27 Ch2:25.52 Ch3:22.15 Ch4:31.56" etc.
現在,我想做的是獲取所有傳入的數據並將其繪制出來。 為此,我創建了具有多個選項卡的Qt應用程序。
Tab 1 - All Sections
Tab 2 - Section A
Tab 3 - Section B
Tab 4 - Section C
Tab 5 - Section D
我向每個選項卡添加了一個小部件,並將其提升為QCustomPlot。
我將每個QCustomPlot設置如下:
// Would be nice to improve this
setupGraph(ui->sectionA); // Setup Section A QCustomPlot
setupGraph(ui->sectionB); // Setup Section B QCustomPlot
setupGraph(ui->sectionC); // Setup Section C QCustomPlot
setupGraph(ui->sectionD); // Setup Section D QCustomPlot
void MainWindow::setupGraph(QCustomPlot *graphPlot)
{
QStringList legend;
legend << "Load Cell 1" << "Load Cell 2" << "Load Cell 3" << "Load Cell 4" << "Total Weight";
graphPlot->legend->setVisible(true);
graphPlot->legend->setFont(QFont("Helvetica",9));
for (int i = 0; i < legend.size(); i++)
{
graphPlot->addGraph();
graphPlot->graph(i)->setName(legend[i]);
graphPlot->graph(i)->setLineStyle(QCPGraph::lsLine);
}
graphPlot->graph(0)->setPen(QPen(Qt::blue));
graphPlot->graph(1)->setPen(QPen(Qt::red));
graphPlot->graph(2)->setPen(QPen(Qt::green));
graphPlot->graph(3)->setPen(QPen(Qt::darkCyan));
graphPlot->axisRect()->setupFullAxesBox();
graphPlot->xAxis->setRange(-10,0);
graphPlot->yAxis->setRange(0,5);
connect(graphPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), graphPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(graphPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), graphPlot->yAxis2, SLOT(setRange(QCPRange)));
}
完成此操作后,我打開串口並連接到ReadyRead信號。 每當有新數據可用時,我都會檢查新數據來自何處,並希望對其進行繪制。
void MainWindow::readData()
{
QByteArray serialData;
if (serial->canReadLine())
serialData = serial->readLine();
if (serialData.startsWith('A'))
realtimePlot(ui->sectionA) // Plot the data for Section A
if (serialData.startsWith('B'))
realtimePlot(ui->sectionB) // Plot the data for Section B
if (serialData.startsWith('C'))
realtimePlot(ui->sectionC) // Plot the data for Section C
if (serialData.startsWith('D'))
realtimePlot(ui->sectionD) // Plot the data for Section D
}
我已經省略了從傳入數據中提取實際值的代碼。
void MainWindow::realtimePlot(QCustomPlot *graphPlot)
{
range_y_min = 0;
range_y_max = 100;
// Add data to the lines
graphPlot->graph(0)->addData(key_x, ch1);
graphPlot->graph(1)->addData(key_x, ch2);
graphPlot->graph(2)->addData(key_x, ch3);
graphPlot->graph(3)->addData(key_x, ch4);
// Remove data outside the visible range
graphPlot->graph(0)->removeDataBefore(key_x-10);
graphPlot->graph(1)->removeDataBefore(key_x-10);
graphPlot->graph(2)->removeDataBefore(key_x-10);
graphPlot->graph(3)->removeDataBefore(key_x-10);
// Make the x-axis range scroll with the data (at a constant range size of 10):
graphPlot->xAxis->setRange(key_x+1/frequency,10,Qt::AlignRight);
// Set the range of the y-axis
graphPlot->yAxis->setRange(range_y_min,range_y_max+5);
// Replot the graph
graphPlot->replot();
key_x += 1/frequency; // defines horizontal gap between two data points on graph
}
現在,我希望removeDataBefore(key_x-10)刪除該點之前的所有數據,因為我發現我的內存很快就被填滿了。 key_x和頻率在其他地方定義。
我目前擁有的代碼(類似於上面的代碼)可以正常工作,但是過一會兒一切開始變慢並且一切都被延遲了。 所以我不太確定是什么問題或導致這種情況的發生。 我還想知道如何使用選項卡1的A部分,B部分,C部分和D部分的圖,因為我不想在第一個選項卡上創建另外4個小部件來繪制數據。
希望我給您足夠的背景信息。
提前謝謝你的幫助。
關於第二個問題,這很容易。 只需將圖形添加到一個小部件(並更改顏色等)
只需將addGraph()調用到一個圖形對象即可。
由於您不會為每個圖形對象調用replot(),因此這也可以提高速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.