从工作线程更新QtCharts的正确方法?

时间:2017-04-05 13:19:45

标签: c++ multithreading data-acquisition qt5.8 qtchart

因为这是我的第一个问题,我想说StackOverflow帮了我无数次。谢谢。

现在我的问题。我目前正在尝试在Qt 5.8中实现一个简单的数据采集应用程序。该应用程序必须与DSP通信并以100Hz至10kHz的速率获取一些电压。由于我需要对获得的电压做一些额外的计算,我认为在与GUI线程不同的线程中进行数据采集和操作是个好主意。

数据采集和附加计算在单独的线程中工作得很好。我的问题是,使用QtCharts异步显示工作线程结果的正确方法是什么?

任何建议都会深表感谢。

致以最诚挚的问候,

T.Krastev

1 个答案:

答案 0 :(得分:0)

有同样的问题。
我有一个线程将数据加载到Model。在它被finshed之后我让线程发出信号DataLoadingDone。这通过MainWindow连接到Qt::QueuedConnection中的一个广告位,因此会从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();
  }
}