PDA

View Full Version : Rappresentation of data on diagram



cecchinoSMI
6th March 2014, 15:08
I'm tryng to solve an issue with data rappresentation, I know perfectly (by debug) where the error accure, but the context is not very simple to define, due to the extension of the code. Starting to explain the work:

The program receives datas from a message broker (activeMq)
Store these value in a string variable
Try to show the values received on a diagram

The relevant code (in my opinion) to understand is this:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap activelogo("C:/Users/Marco/Desktop/CadCamation/Ifacom.JPG");
ui->label_title->setPixmap(activelogo);
setupDiagram();
connectionReceiver();

}
In the previous code be attention to the function setupDiagram implemented here below:

void MainWindow::setupDiagram(){
ui->widget_diagram2->addGraph();
ui->widget_diagram2->graph(0)->setPen(QPen(Qt::blue));
ui->widget_diagram2->graph(0)->setAntialiasedFill(false);
ui->widget_diagram2->xAxis->setTickLabelType(QCPAxis::ltDateTime);
ui->widget_diagram2->xAxis->setDateTimeFormat("hh:mm:ss");
ui->widget_diagram2->xAxis->setAutoTickStep(false);
ui->widget_diagram2->xAxis->setTickStep(2);
ui->widget_diagram2->yAxis->setLabel("Average Wire vibrations[%]");
ui->widget_diagram2->axisRect()->setupFullAxesBox();
connect(ui->widget_diagram2->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->widget_diagram2->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->widget_diagram2->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->widget_diagram2->yAxis2, SLOT(setRange(QCPRange)));
ui->widget_diagram2->replot();
}

After this the diagram is setted in the widget object, and is all ok, so at this point the application wait for a data, and once it is received it is passed to the following function like a double type:

void MainWindow::upDateData(double value0){


double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
static double lastPointKey = 0;
if (key-lastPointKey > 0.01) // at most add point every 10 ms
{

ui->widget_diagram2->graph(0)->addData(key, value0);
ui->widget_diagram2->graph(0)->removeDataBefore(key-8);
ui->widget_diagram2->graph(0)->rescaleValueAxis();
lastPointKey = key;
}
ui->widget_diagram2->xAxis->setRange(key+0.25, 8, Qt::AlignRight);
ui->widget_diagram2->replot();
}

the issue is in the last line, when I try to execute ui->widget_diagram2->replot(), some bad happen and this is the error:

10112

anda_skoa
6th March 2014, 15:53
Looks like upDateData() is not executed by the main thread.

Where do you call that method from?

Cheers,
_

cecchinoSMI
6th March 2014, 15:59
ok I call this method in an other function, this function is an automatic function called by default.
the function is onMessage(const Message* message){.......}...inside this I call upDateData().

anda_skoa
6th March 2014, 18:52
Called by default by what? By the main thread? By another thread?

If it is called by another thread you need to call upDateData() indirectly, e.g. via QMetaObject::invokeMethod(), using Qt::QueuedConnection, or as a signal slot connection using a QueuedConnection.

Cheers,
_

cecchinoSMI
7th March 2014, 07:51
Ok I understand what you mean, so I think that the solution suggested from you is to implement a directConnection or queueConnection to manage two events that evidently are in two different threads.
Thank you!