Correct way to update QtCharts from workflow?
since this is my first question, I wanted to say that StackOverflow has helped me countless times. Thank.
Now to my problem. I am currently trying to implement a simple data collection application in Qt 5.8. The application needs to communicate with the DSP and receive some voltages at a rate of 100 to 10 kHz. Since I need to do some additional calculations on the voltages acquired, I thought it would be nice to do the data collection and manipulation on a different thread than the GUI thread.
Data collection and additional calculations simply work in a separate thread. My question is how to properly display workflow results asynchronously using QtCharts?
Any advice would be deeply appreciated.
Respectfully,
T.Krastev
source to share
The problem is the same.
I have a thread that loads data into Model
. After it's finished, let the stream give a signal DataLoadingDone
. It has to do with the slot in MainWindow
thru Qt::QueuedConnection
, so it is evaluated using GuiThread. Otherwise, I am having problems with the slot QBarSet
throwing the exception.
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();
}
}
source to share