将 lambda 函数作为第三个参数传递给 QObject::connect 时出错
Error passing a lambda function as 3rd argument to QObject::connect
我正在尝试通过将 lambda 函数作为第三个参数传递来调用 QObject::connect
。
但是,我的 Visual Visual Studio 给我以下错误:
Unhandled exception at 0x0000000066B48265 (Qt5Cored.dll) in
QCustomPlotInVS_FirstTry.exe: 0xC0000005: Access violation reading
location 0x0000000000000008.
这是发生错误时调用堆栈中的前 3 行:
Qt5Cored.dll!QListData::size() Line 91
QCustomPlotInVS_FirstTry.exe!QList::size() Line 132 QCustomPlotInVS_FirstTry.exe!QCustomPlot::graph(int index) Line 9657
qList.h 中的第 91 行是:
inline int size() const { return d->end - d->begin; }
我想我收到此错误是因为我错误地尝试将指针(即 QCustomPlot* plot
)与 lambda 函数一起使用。
我的 lambda 函数语法正确吗?
如果不是,我做错了什么?
这是我用 lambda 函数调用 QObject::connect
的函数:
void setupRealTimePlot(QCustomPlot* plot, QTimer* dataTimer)
{
plot->addGraph(); // blue line
plot->graph(0)->setPen(QPen(Qt::blue));
plot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
plot->graph(0)->setAntialiasedFill(false);
plot->addGraph(); // red line
plot->graph(1)->setPen(QPen(Qt::red));
plot->graph(0)->setChannelFillGraph(plot->graph(1));
plot->addGraph(); // blue dot
plot->graph(2)->setPen(QPen(Qt::blue));
plot->graph(2)->setLineStyle(QCPGraph::lsNone);
plot->graph(2)->setScatterStyle(QCPScatterStyle::ssDisc);
plot->addGraph(); // red dot
plot->graph(3)->setPen(QPen(Qt::red));
plot->graph(3)->setLineStyle(QCPGraph::lsNone);
plot->graph(3)->setScatterStyle(QCPScatterStyle::ssDisc);
plot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
plot->xAxis->setDateTimeFormat("hh:mm:ss");
plot->xAxis->setAutoTickStep(false);
plot->xAxis->setTickStep(2);
plot->axisRect()->setupFullAxesBox();
// make left and bottom axes transfer their ranges to right and top axes:
QObject::connect(plot->xAxis, SIGNAL(rangeChanged(QCPRange)), plot->xAxis2, SLOT(setRange(QCPRange)));
QObject::connect(plot->yAxis, SIGNAL(rangeChanged(QCPRange)), plot->yAxis2, SLOT(setRange(QCPRange)));
// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
QObject::connect(dataTimer, &QTimer::timeout,
[&]()
{
// calculate two new data points:
double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
static double lastPointKey = 0;
if (key-lastPointKey > 0.01) // at most add point every 10 ms
{
double value0 = qSin(key); //qSin(key*1.6+qCos(key*1.7)*2)*10 + qSin(key*1.2+0.56)*20 + 26;
double value1 = qCos(key); //qSin(key*1.3+qCos(key*1.2)*1.2)*7 + qSin(key*0.9+0.26)*24 + 26;
// add data to lines:
plot->graph(0)->addData(key, value0);
plot->graph(1)->addData(key, value1);
// set data of dots:
plot->graph(2)->clearData();
plot->graph(2)->addData(key, value0);
plot->graph(3)->clearData();
plot->graph(3)->addData(key, value1);
// remove data of lines that's outside visible range:
plot->graph(0)->removeDataBefore(key-8);
plot->graph(1)->removeDataBefore(key-8);
// rescale value (vertical) axis to fit the current data:
plot->graph(0)->rescaleValueAxis();
plot->graph(1)->rescaleValueAxis(true);
lastPointKey = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
plot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);
plot->replot();
// calculate frames per second:
static double lastFpsKey;
static int frameCount;
++frameCount;
if (key-lastFpsKey > 2) // average fps over 2 seconds
{
lastFpsKey = key;
frameCount = 0;
}
});
dataTimer->start(0); // Interval 0 means to refresh as fast as possible
}
您在稍后将要调用的闭包中通过引用捕获堆栈分配的变量(初犯:plot
);一旦你的关闭被调用,所有这些东西都将成为垃圾。改为按值捕获。
我正在尝试通过将 lambda 函数作为第三个参数传递来调用 QObject::connect
。
但是,我的 Visual Visual Studio 给我以下错误:
Unhandled exception at 0x0000000066B48265 (Qt5Cored.dll) in QCustomPlotInVS_FirstTry.exe: 0xC0000005: Access violation reading location 0x0000000000000008.
这是发生错误时调用堆栈中的前 3 行:
Qt5Cored.dll!QListData::size() Line 91
QCustomPlotInVS_FirstTry.exe!QList::size() Line 132 QCustomPlotInVS_FirstTry.exe!QCustomPlot::graph(int index) Line 9657
qList.h 中的第 91 行是:
inline int size() const { return d->end - d->begin; }
我想我收到此错误是因为我错误地尝试将指针(即 QCustomPlot* plot
)与 lambda 函数一起使用。
我的 lambda 函数语法正确吗? 如果不是,我做错了什么?
这是我用 lambda 函数调用 QObject::connect
的函数:
void setupRealTimePlot(QCustomPlot* plot, QTimer* dataTimer)
{
plot->addGraph(); // blue line
plot->graph(0)->setPen(QPen(Qt::blue));
plot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
plot->graph(0)->setAntialiasedFill(false);
plot->addGraph(); // red line
plot->graph(1)->setPen(QPen(Qt::red));
plot->graph(0)->setChannelFillGraph(plot->graph(1));
plot->addGraph(); // blue dot
plot->graph(2)->setPen(QPen(Qt::blue));
plot->graph(2)->setLineStyle(QCPGraph::lsNone);
plot->graph(2)->setScatterStyle(QCPScatterStyle::ssDisc);
plot->addGraph(); // red dot
plot->graph(3)->setPen(QPen(Qt::red));
plot->graph(3)->setLineStyle(QCPGraph::lsNone);
plot->graph(3)->setScatterStyle(QCPScatterStyle::ssDisc);
plot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
plot->xAxis->setDateTimeFormat("hh:mm:ss");
plot->xAxis->setAutoTickStep(false);
plot->xAxis->setTickStep(2);
plot->axisRect()->setupFullAxesBox();
// make left and bottom axes transfer their ranges to right and top axes:
QObject::connect(plot->xAxis, SIGNAL(rangeChanged(QCPRange)), plot->xAxis2, SLOT(setRange(QCPRange)));
QObject::connect(plot->yAxis, SIGNAL(rangeChanged(QCPRange)), plot->yAxis2, SLOT(setRange(QCPRange)));
// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
QObject::connect(dataTimer, &QTimer::timeout,
[&]()
{
// calculate two new data points:
double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
static double lastPointKey = 0;
if (key-lastPointKey > 0.01) // at most add point every 10 ms
{
double value0 = qSin(key); //qSin(key*1.6+qCos(key*1.7)*2)*10 + qSin(key*1.2+0.56)*20 + 26;
double value1 = qCos(key); //qSin(key*1.3+qCos(key*1.2)*1.2)*7 + qSin(key*0.9+0.26)*24 + 26;
// add data to lines:
plot->graph(0)->addData(key, value0);
plot->graph(1)->addData(key, value1);
// set data of dots:
plot->graph(2)->clearData();
plot->graph(2)->addData(key, value0);
plot->graph(3)->clearData();
plot->graph(3)->addData(key, value1);
// remove data of lines that's outside visible range:
plot->graph(0)->removeDataBefore(key-8);
plot->graph(1)->removeDataBefore(key-8);
// rescale value (vertical) axis to fit the current data:
plot->graph(0)->rescaleValueAxis();
plot->graph(1)->rescaleValueAxis(true);
lastPointKey = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
plot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);
plot->replot();
// calculate frames per second:
static double lastFpsKey;
static int frameCount;
++frameCount;
if (key-lastFpsKey > 2) // average fps over 2 seconds
{
lastFpsKey = key;
frameCount = 0;
}
});
dataTimer->start(0); // Interval 0 means to refresh as fast as possible
}
您在稍后将要调用的闭包中通过引用捕获堆栈分配的变量(初犯:plot
);一旦你的关闭被调用,所有这些东西都将成为垃圾。改为按值捕获。