首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图表上数据的表示

图表上数据的表示
EN

Stack Overflow用户
提问于 2014-03-06 15:01:54
回答 1查看 110关注 0票数 2

我试图解决数据表示的一个问题,我非常清楚(通过调试)错误发生的地方,但是由于代码的扩展,上下文的定义并不简单。开始解释这项工作:

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

有关的守则(我认为)是:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
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();
}

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

代码语言:javascript
复制
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()函数的请求方式:

代码语言:javascript
复制
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 );
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-15 11:08:42

据我所知,您的MainWindow是消息侦听器,因此接收数据并将其直接转发给upDateData,对吗?我还假设数据来自工作线程,而不是主UI线程。

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

(考虑一下,在UI线程试图呈现相同文本的同时,从工作线程更改标签文本会发生什么情况。)

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

代码语言:javascript
复制
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)));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22228184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档