(TOFU to keep full version of original solution without hiding my reply)

I am not sure how your initial project looks, but for the code you sent, I would suggest a different approach:

class Worker
{
  public:
    int doHeavyStuff()
    {
      return 42;
    }
};


class Handler
{
  public:
    Handler()
      : m_worker( new Worker )
    { }

    QFuture<int> meaningOfLife()
    {
      return QtConcurrent::run( m_worker, &Worker::doHeavyStuff );
    }

  private:
    Worker *m_worker;
};

NOTE: This code snippet is untested, it is based on this article: http://doc.qt.io/qt-4.8/qtconcurrentrun.html

If your application is as simple as above, you might even drop the Handler class (as it does almost nothing now).


Am 10.07.2015 um 12:51 schrieb Lorenz Haas:
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?
[...]

Best Regards / Mit freundlichen Grüßen
Rainer Wiesenfarth

--
Software Engineer | Trimble Imaging Division
Rotebühlstraße 81 | 70178 Stuttgart | Germany
Office +49 711 22881 0 | Fax +49 711 22881 11
http://www.trimble.com/imaging/ | http://www.inpho.de/

Trimble Germany GmbH, Am Prime Parc 11, 65479 Raunheim
Eingetragen beim Amtsgericht Darmstadt unter HRB 83893,
Geschäftsführer: Dr. Frank Heimberg, Hans-Jürgen Gebauer

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to