Paste

URL To paste - | raw - Wed Feb 21 2018 22:22:32 GMT+0000 (Coordinated Universal Time)
#include <QCoreApplication>
#include <QEventLoop>
#include <QTemporaryFile>
#include <QDebug>
#include <QTimer>
#include <QTime>
#include <future>

int main(int argc, char *argv[])
{
  QCoreApplication a(argc, argv);

  // This timer merely proves that the main loop stays active.
  // Look at the console log, you should see a time print every 100ms.
  // Try potting a sleep in the async lambda below, you will see that you still get a time dump every 100ms (that is good!)
  QTimer demo;
  demo.setInterval(100);
  QObject::connect(&demo, &QTimer::timeout, []()
  {
    qDebug() << QTime::currentTime();
  });
  demo.start();

  // Just a temp file. You don't need it in your patch for KIO.
  QTemporaryFile tempFile;
  tempFile.setAutoRemove(false);
  tempFile.open();
  QEventLoop loop;
  QString fullFilePath = tempFile.fileName();

  auto deleteResult = std::async(std::launch::async, [fullFilePath, &loop]()
  {
    // Get the result of deleting the file, we ruturn this later on.
    bool result = QFile::remove(fullFilePath);

    // Quit the eventloop instance, this unblocks the function that called exec on that loop object.
    loop.quit();

    // Return whatever the result was. Should be checked in the parent function.
    return result;
  });

  // This blocks the function at _this_ point till quit() is called on the loop.
  loop.exec();

  // We get here if quit() was called. Get the value from the std::async return (the bool of the delete call)
  // And do whatever checks you want.
  if (deleteResult.get())
  {
    qDebug() << "file removed";
  }
  else
  {
    qDebug() << "File is still there...";
  }

  return a.exec();
}