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

QThread:create syntax



  • I am trying the new QThread::create introduced in 5.10 for the first time, but I think I have syntax issues. I want to run a threaded function called "process" in my worker class and pass two arguments with it but it can not link to the public slot in the worker thread as it is not yet defined; I am using Creator 4.7.2 and QT 5.11.2

    QThread *thread = QThread::create(process(data,list));  // has no reference to validate that "process" exists
        parameters_workerThread* worker = new parameters_workerThread();
        worker->moveToThread(thread);
    

    So I re-arranged my code and added the reference to the worker class but gave me an invalid use of void.

    parameters_workerThread* worker = new parameters_workerThread();  // invalid use of void
        QThread *thread = QThread::create(worker->process(data,list));
        worker->moveToThread(thread);
    

    My worker header file looks like this:

    class parameters_workerThread : public QObject
    {
        Q_OBJECT
    public:
        
        parameters_workerThread();
        ~parameters_workerThread();
    
    public slots:
        void process(QByteArray list, QStringList data);
    

    I am compiling with: MingW32- gcc version 5.1.0 (tdm64-1)
    Any direction would be most appreciated



  • The answer is std::bind

    parameters_workerThread* worker = new parameters_workerThread();
    QThread *thread = QThread::create(std::bind(&parameters_workerThread::process,worker,data,list));
    worker->moveToThread(thread);
    thread->setParent(this);
    thread->start();
    


  • Perfect !!!! What a group !!! Thanks very much, this save me a lot work !!!

    This is an awesome new feature and now I can use it.

    Rick


Log in to reply