因为这是我的第一个问题,所以我想说 StackOverflow 帮助了我无数次。谢谢。
现在是我的问题。我目前正在尝试在 Qt 5.8 中实现一个简单的数据采集应用程序。该应用程序必须与 DSP 通信并以 100Hz 至 10kHz 的速率获取一些电压。由于我需要对获取的电压进行一些额外的计算,我认为在与 GUI 线程不同的线程中进行数据采集和操作是个好主意。
数据采集和附加计算在单独的线程中工作得很好。我的问题是,使用 QtCharts 异步显示工作线程结果的正确方法是什么?
如有任何建议,我们将不胜感激。
最好的问候,
T.Krastev
最佳答案
遇到了同样的问题。
我有一个将数据加载到 Model
的线程。完成后,我让线程发出信号 DataLoadingDone
。这通过 Qt::QueuedConnection
连接到 MainWindow
中的插槽,因此它是从 GuiThread 评估的。否则我会遇到 QBarSet
槽抛出异常的问题。
MainWindow::MainWindow() {
this->chart = new QChart();
this->chartView = new QChartView(chart);
this->series = new QBarSeries();
this->mapper = new QHBarModelMapper(this);
this->connect(this->myThread, SIGNAL(DataLoadingDone()),
this, SLOT(MyThread_DataLoadingDone()),
Qt::QueuedConnection);
this->setWidget(this->chartView);
}
void MainWindow::MyThread_DataLoadingDone() {
mapper->setFirstBarSetRow(0);
mapper->setLastBarSetRow(0);
mapper->setFirstColumn(0);
mapper->setColumnCount(this->model->columnCount());
mapper->setSeries(series);
mapper->setModel(this->model);
//only add at the first time
//if we add this every time something goes wrong and
// multiple bars are displayed behind each other
if (this->chart->series().count() == 0) {
this->chart->addSeries(series);
this->chart->createDefaultAxes();
}
}
关于c++ - 从工作线程更新 QtCharts 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43232456/