Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to call functor with QThread



  • I have a class with an operator() overloaded.

    class MyWorker : public QObject
    {
        Q_OBJECT
        private:
        // ...
        signals:
            void t_changed(const QString &);
        public:
        void operator()();
            // ...
    };
    

    How does one call the functor of MyWorker using QThread?

    my_worker = std::make_unique<MyWorker>(this);
    // ...
    
    QThread *my_thread = new QThread;
    my_worker->moveToThread(my_thread);
    my_thread->start(); // I want MyWorker::operater()() to be called.
    

    Any help much appreciated.


  • Lifetime Qt Champion

    Hi,

    If you want to use QThread, you should rather use the working object approach as described in QThread's documentation. Replace that operator with a slot as shown in said documentation.


Log in to reply