[Interest] QFutureWatcher and qRegisterMetaType

Lorenz Haas lykurg at gmail.com
Fri Jul 10 12:51:33 CEST 2015


Hi,

I try to use QFuture/QFutureWatcher for my own project. So far I have this
simplified setting, in which Handler is living in the GUI thread:

class Worker : public QObject
{
  Q_OBJECT

  public slots:
    void doHeavyStuff(QFutureInterface<int> inter) {
      inter.reportResult(42);
      inter.reportFinished();
    }
};

class Handler : public QObject
{
  Q_OBJECT

  public:
    Handler(QObject *parent = 0) :
      QObject(parent),
      m_worker(new Worker)
    {
      m_worker->moveToThread(&m_thread);
      connect(&m_thread, &QThread::finished, m_worker,
&QObject::deleteLater);
      m_thread.start();
    }

    QFuture<int> meaningOfLife()
    {
      QFutureInterface<int> inter;
      inter.reportStarted();
      QMetaObject::invokeMethod(m_worker, "doHeavyStuff",
Qt::QueuedConnection, Q_ARG(QFutureInterface<int>, inter));
      return inter.future();
    }

  private:
    Worker *m_worker;
    QThread m_thread;
};

This works fine if I add "qRegisterMetaType<QFutureInterface<int>
>("QFutureInterface<int>");" somewhere. This, however, makes me nervous
because it feels like I have not to do this. It's a pure paranoid feeling...
Therefore:
  a) Is the approach above okay?
  b) Why QFutureInterface isn't registered already by Qt? Is it because one
can't register templated classes easily?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150710/607e1c13/attachment.html>


More information about the Interest mailing list