在图上表示数据

时间:2014-03-06 15:01:54

标签: c++ visual-c++ qt5.1

我正在尝试解决数据表示的问题,我完全知道(通过调试)发生错误的地方,但由于代码的扩展,上下文的定义并不是很简单。开始解释这项工作:

  • 程序从消息代理(activeMq)
  • 接收数据
  • 将这些值存储在字符串变量
  • 尝试显示图表上收到的值

相关代码(在我看来)要理解的是:

#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();

}

在前面的代码中,请注意下面实现的函数setupDiagram:

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();
}

在此之后,图表在widget对象中设置,并且一切正常,所以此时应用程序等待数据,一旦收到它,它就会像双重类型一样传递给以下函数:

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(); <---error here
}

问题出在最后一行,当我尝试执行ui->widget_diagram2->replot()时,发生了一些不好的事情,这就是错误:

enter image description here

如何请求这是connectionReceiver()函数:

void MainWindow::connectionReceiver(){

     activemq::library::ActiveMQCPP::initializeLibrary();

     // Set the URI to point to the IP Address of your broker.
     //std::string brokerURI = "tcp://92.104.242.137:61613?wireFormat=stomp";   // remote
     std::string brokerURI = "tcp://localhost:61613?wireFormat=stomp";      // localhost

    // Queue name
     std::string destName = "IFACOM-CMS";

     // Queue or Topic 
     bool useTopics = false;            // true=Topic, false=Queue

     // SESSION_TRANSACTED or AUTO_ACKNOWLEDGE
     bool sessionTransacted = false; 



     long long startTime = System::currentTimeMillis();

     // ***** Initialisation  **************************************************************
     m_IfacomAmqListener = new IfacomAmqReceiver(brokerURI,useTopics,destName,sessionTransacted);
     m_IfacomAmqListener->initConnection();

     m_IfacomAmqListener->setMessageListener( this );
}

1 个答案:

答案 0 :(得分:0)

据我了解,你的MainWindow是消息监听器,因此接收数据并将其直接转发到upDateData,对吧?我还假设数据来自工作线程,而不是主UI线程。

大多数Qt类不是线程安全的,因此通常从工作线程中调用它们上的任何成员函数是不安全的。

(考虑如果在UI线程尝试呈现相同文本的同时从工作线程更改标签文本会发生什么。)

在您的情况下,您现在必须将传入数据与小部件上的调用成员分离,这是使用信号/插槽机制最简单的方法。

MainWindow::slot_upDateData(double value) {
  // process value
}

void MainWindow::upDateData(double value0) {
  emit sig_onUpDateData(value0);
}

// If upDateData is called from within the UI thread the slot gets called
// directly. If it is called from within a worker thread then a queued 
// connection will be used. In both cases the slot gets entered from within the
// the UI thread.
connect(this, SIGNAL(sig_onUpDateData(double)), SLOT(slot_upDateData(double)));
相关问题