有人可以建议如何正确实施SLOT执行吗?
我的代码:
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
}
}
以下是我的发现:
SIGNAL和SLOT需要相同的签名,构建程序不会运行 SLOT(因为SLOT变得未定义)。
可能的解决方案:从SLOT中删除QCustomPlot参数,但是如何 然后我应该发送到QCustomPlot的realtimeDataSlot指针?也许 有可能超载timeout()?也许是其他解决方案?
我发现当我使用#include“winsock2.h”并尝试“提升为...”选项时 比如http://www.qcustomplot.com/index.php/tutorials/settingup错误 出现关于参数重新定义,所以这种解决方法我不能 使用。我也不会不使用qwt
谢谢你的帮助!
答案 0 :(得分:1)
有很多解决方案。我想到了两个:
让QCustomPlot*
成为prog
类的成员:
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());
}
使用C ++ 11和Qt 5功能:
connect(&dataTimer, &QTimer::timeout, this, [=]{
realtimeDataSlot(customPlot); // doesn't need to be a slot anymore
});