在连接信号/时隙中使用C++关键字 5.9,我尝试用C++关键字代替时隙。如果没有单独的方法,这是可能的吗

在连接信号/时隙中使用C++关键字 5.9,我尝试用C++关键字代替时隙。如果没有单独的方法,这是可能的吗,c++,qt,slot,C++,Qt,Slot,比如: QObject::connect(&timer, SIGNAL(timeout()), this, (delete image_ptr)); 这不起作用,下面是我的代码示例: QImage *image_ptr = new QImage(3, 3, QImage::Format_Indexed8); QEventLoop evt; QFutureWatcher<QString> watcher; QTimer timer(this); timer.setSingl

比如:

QObject::connect(&timer, SIGNAL(timeout()),  this, (delete image_ptr));
这不起作用,下面是我的代码示例:

QImage *image_ptr = new QImage(3, 3, QImage::Format_Indexed8);
QEventLoop evt;
QFutureWatcher<QString> watcher;
QTimer timer(this);
timer.setSingleShot(true);
QObject::connect(&watcher, &QFutureWatcher<QString>::finished, &evt, &QEventLoop::quit);
QObject::connect(&timer, SIGNAL(timeout()), &watcher, SLOT(cancel()));
QObject::connect(&timer, SIGNAL(timeout()),  &evt, SLOT(quit));
QObject::connect(&timer, SIGNAL(timeout()),  this, (delete image_ptr));
QFuture<QString> future = QtConcurrent::run(this,&myClass::myMethod,*image_ptr);
watcher.setFuture(future);
timer.start(100);
evt.exec();

连接示例中的lambda表达式:

connect(
    sender, &Sender::valueChanged,
    [=]( const QString &newValue ) { receiver->updateValue( "senderValue", newValue ); }
);

连接示例中的lambda表达式:

connect(
    sender, &Sender::valueChanged,
    [=]( const QString &newValue ) { receiver->updateValue( "senderValue", newValue ); }
);

您可以使用lambda表达式来代替新的connect语法

考虑到这些示例中的接收方是插槽的所有者,如果使用旧语法,则它不是全局变量或宏。另外,当使用lambdas时,您没有对sender方法的访问权限,因此必须注意通过其他方法访问它们。要解决这些情况,必须在lambda中捕获这些变量。在您的情况下,它只是指针

QObject::connect(&timer, &QTimer::timeout, [&image_ptr]() { delete image_ptr; });

您可以使用lambda表达式来代替新的connect语法

考虑到这些示例中的接收方是插槽的所有者,如果使用旧语法,则它不是全局变量或宏。另外,当使用lambdas时,您没有对sender方法的访问权限,因此必须注意通过其他方法访问它们。要解决这些情况,必须在lambda中捕获这些变量。在您的情况下,它只是指针

QObject::connect(&timer, &QTimer::timeout, [&image_ptr]() { delete image_ptr; });

使用lambda。尽管如此,这看起来很危险。我的意思是,如果并发执行仍在运行,并且您释放了映像,那么就会发生不好的事情..使用lambda。尽管如此,这看起来很危险。我的意思是,如果并发执行仍在运行,并且您释放了映像,那么就会发生不好的事情。。