Qthread problem.



  • Why I get this error:
    moc_mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall Worker::doWork(void)" (?doWork@Worker@@QAEXXZ) referenced in function "private: static void __cdecl Worker::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@Worker@@CAXPAVQObject@@W4Call@QMetaObject@@HPAPAX@Z)

    mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Worker::Worker(void)" (??0Worker@@QAE@XZ) referenced in function "private: void __thiscall MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked@MainWindow@@AAEXXZ)

    mainwindow.h:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QThread>
    namespace Ui {
    class MainWindow;
    class Worker;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    Ui::MainWindow *ui;
    void setText();

    private slots:
    void on_pushButton_clicked();

    private:

    };
    class Worker : public QThread
    {

    Q_OBJECT
    public:
    Worker();
    ~Worker(void);
    virtual void run();
    public slots:
    void doWork() ;
    };
    #endif // MAINWINDOW_H@

    main.cpp
    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QThread>
    #include <iostream>
    using namespace std;
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    void MainWindow::setText(){
    ui->label->setText("few");

    }

    void MainWindow::on_pushButton_clicked()
    {
    /* ... */
    QThread *thread = new QThread;
    Worker *worker = new Worker;
    //obj is a pointer to a QObject that will trigger the work to start. It could just be this

    worker->start();
    

    }

    void Worker::run()
    {
    cout << "done";
    MainWindow *a = new MainWindow;
    a->setText();
    }@



  • Try re-running qmake on your project.



  • Nothing new.



  • Create an implementation for doWork and the constructor of your Worker class.



  • By the way: it looks like you are using QThread in the wrong way.

    You are creating a worker that is both derived from QThread as well as moved into one. On top of that, the worker has a slot which will be executed in the context of the thread the Worker object lives in, not the thread that the Worker object manages.

    It looks like you wrote a hybrid of two possible correct approaches, creating something that almost certainly won't do what you want or think it will.



  • Ok.But how to set a label in my form from the thread?



  • First, please clean up your worker/threading code to match the "doc note":/doc/qt-4.7/qthread.html#note-5 on this issue in QThread. Then, create a signal on your worker object with the new string as your argument. Then, connect this signal to the label's setText slot. Under no condition call setText on the label directly from your thread!



  • Here is a small example of using threads the right way as suggested by Andre.

    Worker.h

    @class Worker : public QObject
    {
    Q_OBJECT
    public:
    explicit Worker(QObject *parent = 0);

    signals:
    void finished();
    void numberChanged(int i);

    public slots:
    void process();

    };@

    .cpp

    @Worker::Worker(QObject *parent) :
    QObject(parent)
    {
    }

    void Worker::process()
    {
    for(int i=0; i < 1000000; i++)
    {
    qDebug() << i;
    emit numberChanged(i);
    }

    emit finished();
    

    }@

    MainWindow.h

    @class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();
    void onNumberChanged(int num);

    private:
    Ui::MainWindow *ui;
    };@

    .cpp

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    QThread *thread = new QThread();
    Worker *worker = new Worker();
    worker->moveToThread(thread);

    connect(thread, SIGNAL(started()), worker, SLOT(process()));
    connect(worker, SIGNAL(numberChanged(int)),this,SLOT(onNumberChanged(int)));
    connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    connect(thread, SIGNAL(finished()), thread , SLOT(deleteLater()));
    
    thread->start();
    

    }

    void MainWindow::onNumberChanged(int num)
    {
    ui->label->setText(QString::number(num));
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.