0
//functions for plotting graph
void MainWindow::opengraph()
{
    QDialog* graphdialog = new QDialog;
    Ui::graph ui_graph ;
    ui_graph.setupUi(graphdialog);

    setGeometry(400, 250, 542, 390);
    setWindowTitle("Graph");
    //statusBar()->clearMessage();
    //currentDemoIndex = demoIndex;
   ui_graph.customPlot->replot();

    RealtimeDataGraph(ui_graph.customPlot);
}

void MainWindow::RealtimeDataGraph(QCustomPlot *customPlot)
{
#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
  QMessageBox::critical(this, "", "You're using Qt < 4.7, the realtime data demo needs functions that are available with Qt 4.7 to work properly");
#endif
  //demoName = "Real Time Data Demo";

  // include this section to fully disable antialiasing for higher performance:
  /*
  customPlot->setNotAntialiasedElements(QCP::aeAll);
  QFont font;
  font.setStyleStrategy(QFont::NoAntialias);
  customPlot->xAxis->setTickLabelFont(font);
  customPlot->yAxis->setTickLabelFont(font);
  customPlot->legend->setFont(font);
  */
  customPlot->addGraph(); // blue line
  customPlot->graph(0)->setPen(QPen(Qt::blue));
  customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
  customPlot->graph(0)->setAntialiasedFill(false);
  customPlot->addGraph(); // red line
  customPlot->graph(1)->setPen(QPen(Qt::red));
  customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));
  //
//  customPlot->addGraph(); // green line
//  customPlot->graph(2)->setPen(QPen(Qt::green));
//  customPlot->graph(0)->setChannelFillGraph(customPlot->graph(2));
  //

  customPlot->addGraph(); // blue dot
  customPlot->graph(2)->setPen(QPen(Qt::blue));
  customPlot->graph(2)->setLineStyle(QCPGraph::lsNone);
  customPlot->graph(2)->setScatterStyle(QCP::ssDisc);
  customPlot->addGraph(); // red dot
  customPlot->graph(3)->setPen(QPen(Qt::red));
  customPlot->graph(3)->setLineStyle(QCPGraph::lsNone);
  customPlot->graph(3)->setScatterStyle(QCP::ssDisc);

//  customPlot->addGraph(); // green dot
//  customPlot->graph(5)->setPen(QPen(Qt::green));
//  customPlot->graph(5)->setLineStyle(QCPGraph::lsNone);
//  customPlot->graph(5)->setScatterStyle(QCP::ssDisc);
//
  customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
  customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
  customPlot->xAxis->setAutoTickStep(false);
  customPlot->xAxis->setTickStep(2);
  customPlot->setupFullAxesBox();
  customPlot->xAxis->setLabel("Time(Sec)");
  customPlot->yAxis->setLabel("magnitude");

  // make left and bottom axes transfer their ranges to right and top axes:
  connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
  connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

  // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
  *connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot(QCustomPlot *)));*
  dataTimer.start(0); // Interval 0 means to refresh as fast as possible
}

void MainWindow::realtimeDataSlot(QCustomPlot *customPlot)
{
  // calculate two new data points:
#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
  double key = 0;
#else
  double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
#endif
  static double lastPointKey = 0;
  if (key-lastPointKey > 0.01) // at most add point every 10 ms
  {
    double value0 = sin(key*1.6+cos(key*1.7)*2)*10 + sin(key*1.2+0.56)*20 + 26;
    double value1 = sin(key*1.3+cos(key*1.2)*1.2)*7 + sin(key*0.9+0.26)*24 + 26;
    //
    //double value2 = sin(key*2+cos(key*2.2)*3)*17 + sin(key*2.3+0.76)*30 + 26;
    //
    // add data to lines:
    customPlot->graph(0)->addData(key, value0);
    customPlot->graph(1)->addData(key, value1);
    //
//    ui->customPlot->graph(1)->addData(key, value2);
    // set data of dots:
    customPlot->graph(2)->clearData();
    customPlot->graph(2)->addData(key, value0);
    customPlot->graph(3)->clearData();
    customPlot->graph(3)->addData(key, value1);

//    ui->customPlot->graph(5)->clearData();
//    ui->customPlot->graph(5)->addData(key, value2);
    // remove data of lines that's outside visible range:
    customPlot->graph(0)->removeDataBefore(key-20);
    customPlot->graph(1)->removeDataBefore(key-20);
//    ui->customPlot->graph(2)->removeDataBefore(key-20);
    // rescale value (vertical) axis to fit the current data:
    customPlot->graph(0)->rescaleValueAxis();
    customPlot->graph(1)->rescaleValueAxis(true);
//    ui->customPlot->graph(2)->rescaleValueAxis(true);
    lastPointKey = key;
  }
  // make key axis range scroll with the data (at a constant range size of 8):
  customPlot->xAxis->setRange(key+0.25, 20, Qt::AlignRight);
  customPlot->replot();

  // calculate frames per second:
  static double lastFpsKey;
  static int frameCount;
  ++frameCount;
  if (key-lastFpsKey > 2) // average fps over 2 seconds
  {
    //ui->statusBar->showMessage(
//          QString("%1 FPS, Total Data points: %2")
//          .arg(frameCount/(key-lastFpsKey), 0, 'f', 0)
//          .arg(ui->customPlot->graph(0)->data()->count()+ui->customPlot->graph(1)->data()->count())
//          , 0);
    lastFpsKey = key;
    frameCount = 0;
  }
}

In the above code, I want to call a graph dialog from a main window, when a pushbutton is pressed. When I run this code, in function RealtimeDataGraph()

connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot(QCustomPlot *)));

at this line, compiler says incompatible sender receiver.

I think, when data timer timeouts a signal should received by graph dialog. Is it received by it or not, I am not able to understand. Can please anybody review this code and let me know how to call the slot realtimeDataSlot()?

Mat
  • 202,337
  • 40
  • 393
  • 406
Amar
  • 379
  • 2
  • 5
  • 18
  • connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot(QCustomPlot *))); SIGNAL and SLOT must have same parameter. – Lwin Htoo Ko Dec 20 '12 at 06:31

1 Answers1

0
QCustomPlot * ptr_CustomPlot; // pointer of QCustomPlot

//functions for plotting graph
void MainWindow::opengraph()
{
    QDialog* graphdialog = new QDialog;
    Ui::graph ui_graph ;
    ui_graph.setupUi(graphdialog);

    setGeometry(400, 250, 542, 390);
    setWindowTitle("Graph");
    ui_graph.customPlot->replot();
    RealtimeDataGraph(ui_graph.customPlot);
}

void MainWindow::RealtimeDataGraph(QCustomPlot *customPlot)
{
  ptr_CustomPlot = customPlot; // here, customPlot is saved to ptr_CustomPlot
  /* ... */

  // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
  connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));

}

void MainWindow::realtimeDataSlot()
{
  /* use ptr_CustomPlot // here, ptr_CustomPlot is the same as customPlot which is also ui_graph.customPlot */
  /* ... */
}

Slot and Signal must have the same number of arguments and types.

Lwin Htoo Ko
  • 2,326
  • 4
  • 26
  • 38
  • Thank you Lwin Htoo Ko!Now the thing is QCustomPlot *customPlot is defined in ui_graph.How use it in realtimeDataSlot().actually customplot is a widget,which is promoted to QCustomPlot.In this widget I am trying to plot graph. – Amar Dec 20 '12 at 06:46
  • while calling RealtimeDataGraph() i am passing Customplot so no problem in this function as customplot is member of Ui_graph.but how access same member of Ui-graph in realtimeDataSlot(). – Amar Dec 20 '12 at 06:55
  • "ptr_CustomPlot = customPlot;" So, ptr_CustomPlot is the same as ui_graph.customPlot. In realtimeDataSlot(), you can use ptr_CustomPlot which is the same as ui_graph.customPlot. – Lwin Htoo Ko Dec 20 '12 at 07:31