【问题标题】:QCustomPlot - Plots in various tabsQCustomPlot - 各种选项卡中的绘图
【发布时间】:2014-09-25 05:02:47
【问题描述】:

我目前正在通过串行通信接收信息,请参阅下面的数据输入示例:

"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 部分的图,因为我不想在第 1 个选项卡上创建另外 4 个小部件来绘制数据。

希望我已经为您提供了足够的背景信息。

提前感谢您的帮助。

【问题讨论】:

    标签: c++ qt qcustomplot


    【解决方案1】:

    关于你的第二个问题,很简单。 只需将图表添加到一个小部件(并可能更改颜色等)

    只需调用 addGraph() 到一个图形对象。

    这也可能会提高您的速度,因为您不会为每个图形对象调用 replot()。

    【讨论】:

    • 我知道我可以将多个图表添加到一个小部件,但我不太确定如何在一个小部件上获得散点图和正常图,然后如何让它们都在即时的。无论如何,为了加快速度,我使用了一个计时器并每隔一段时间调用一次。似乎加快了一点。
    • 你想在同一个图中散点图和法线图还是同一个小部件?我遇到了类似的问题,我使用了一个单独的线程来读取数据(在你的情况下是从 com 端口串行),然后该线程向必要的窗口发出信号以进行更新。这样我只在新数据进来时更新,而不是基于任意计时器。
    • 我想在同一个小部件中有一个散点图和一个普通图(所以只有一个 QWidget 提升为 QCustomPlot 并且在该小部件中都有两个图)。那将是一个很好的解决方案,我以前从未使用过线程,所以我不知道如何使用单独的线程来读取数据和另一个线程来更新必要的窗口。你知道有什么好的链接可以帮助我开始吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2016-11-06
    • 1970-01-01
    相关资源
    最近更新 更多