gen_next
gen_next

Reputation: 97

SIGNAL and SLOT parameters issue in QT

Could someone please advise how to correctly implement SLOT execution?

my code:

prog::prog(QWidget *parent): QMainWindow(parent) //constructor (VisualStudio):
{
    ui.setupUi(this);
    QCustomPlot * customPlot = new QCustomPlot(this);

    setupRealtimeDataDemo(customPlot);

    // + more code
}

void prog::setupRealtimeDataDemo(QCustomPlot * customPlot)
{
 customPlot->addGraph(); // 
// + more related with graph methods

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

void prog::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 = qSin(key); //sin(key*1.6+cos(key*1.7)*2)*10 + sin(key*1.2+0.56)*20 + 26;
    double value1 = qCos(key); //sin(key*1.3+cos(key*1.2)*1.2)*7 + sin(key*0.9+0.26)*24 + 26
      // add data to lines:
    customPlot->graph(0)->addData(key, value0);
    customPlot->graph(1)->addData(key, value1);
// + more code related with graph
}
}

Here are my findings:

  1. SIGNAL and SLOT need the same signature, build program won't run SLOT ( because SLOT become undefined).

    Possible solution: remove QCustomPlot argument form SLOT, but how then should I send to realtimeDataSlot pointer to QCustomPlot? Maybe it possible to overload timeout() ? Maybe other solution?

  2. I discovered when I use #include "winsock2.h" and try to "Promote to..." option like in http://www.qcustomplot.com/index.php/tutorials/settingup errors appears about parameters redefinition , so this workaround I cannot use. I also don't won't to use qwt

Thanks is advance for help!

Upvotes: 1

Views: 545

Answers (1)

There is a multitude of solutions. Two come to mind:

  1. Make the QCustomPlot* a member of the prog class:

    class prog : public QWidget {
      Q_OBJECT
      QScopedPointer<QCustomPlot> m_plot;
      ...
    }
    
    prog::prog(QWidget *parent): QMainWindow(parent) :
      m_plot(new QCustomPlot)
    {
      ui.setupUi(this);
      setupRealtimeDataDemo(m_plot.data());
    }
    
  2. Use C++11 and Qt 5 features:

    connect(&dataTimer, &QTimer::timeout, this, [=]{
      realtimeDataSlot(customPlot); // doesn't need to be a slot anymore
    });
    

Upvotes: 1

Related Questions