2

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

  • 程序从消息代理(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();
}

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

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(),发生了一些不好的事情,这是错误:

在此处输入图像描述

这是如何请求的 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 );
}
4

1 回答 1

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)));
于 2014-03-15T11:08:42.457 回答