In Qt 5.9, I am trying to use C++ keywords in place of SLOT. Is that possible (without a separate method)?
Something like:
QObject::connect(&timer, SIGNAL(timeout()), this, (delete image_ptr));
That is not working, below my code example:
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();
You can use a lambda expression instead (more about the new connect syntaxis here).
Take into consideration that receiver in those examples is the-otherwise-owner of the slot (if used the old syntax), it is not a global variable or macro. Also, when using lambdas you don't have access to the sender()
method, so you must take care of having access to them through other method. To solve those situations you have to capture those variables in the lambda. In your case, it is only the pointer.
QObject::connect(&timer, &QTimer::timeout, [&image_ptr]() { delete image_ptr; });