如何从qt中的mainwindow调用对话框?

时间:2012-12-20 06:20:44

标签: qt qt4 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");
    //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;
  }
}

在上面的代码中,我想在按下按钮时从主窗口调用图形对话框。当我在函数RealtimeDataGraph()

中运行此代码时
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot(QCustomPlot *)));

在这一行,编译器说不兼容的发送方接收方。

我认为,当数据定时器超时时,应通过图形对话框接收信号。它是否被收到,我无法理解。可以请任何人查看此代码并告诉我如何拨打电话realtimeDataSlot()

1 个答案:

答案 0 :(得分: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和Signal必须具有相同数量的参数和类型。