Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qthread problem.
Qt 6.11 is out! See what's new in the release blog

Qthread problem.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dwsld
    wrote on last edited by
    #1

    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();
    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Try re-running qmake on your project.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dwsld
        wrote on last edited by
        #3

        Nothing new.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dwsld
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  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));
                  }@

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved