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. Signal/Slot doesn't work after moveToThread
Qt 6.11 is out! See what's new in the release blog

Signal/Slot doesn't work after moveToThread

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.3k 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.
  • R Offline
    R Offline
    rubikon
    wrote on last edited by
    #1

    I have implemented this approach to decouple the GUI from the Businesslayer:

    main.c
    @
    #include "Core.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Dialog w;
    Core *core = new Core(&w);
    w.show();

    return a.exec();
    }
    @

    IGui
    @
    class IGui
    {
    //--- slots
    public:
    virtual void onWorkFinished() = 0;

    //--- signals
    signals:
    void doWork();
    };
    @

    Core
    @
    #include "IGui.h"

    class Core : public QObject
    {
    Q_OBJECT

    public:
    explicit Core(IGui *gui, QObject *parent = 0);

    signals:
    void workFinished();

    private slots:
    void onDoWork();

    private:
    QThread m_workerThread;

    };

    Core::Core(IGui *gui, QObject *parent) :
    QObject(parent)
    {
    connect(dynamic_cast<QObject *>(gui), SIGNAL(doWork()),
    this, SLOT(onDoWork()));
    connect(this, SIGNAL(workFinished()),
    dynamic_cast<QObject *>(gui), SLOT(onWorkFinished()));

    //this->moveToThread(&m_workerThread);
    }

    void Core::onDoWork()
    {
    qDebug() << Q_FUNC_INFO;
    QThread::msleep(1000);
    emit workFinished();
    }
    @

    Dialog
    @
    #include "IGui.h"

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog, public IGui
    {
    Q_OBJECT

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

    public slots:
    void onWorkFinished();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::Dialog *ui;

    signals:
    void doWork();
    };

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    }

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

    void Dialog::onWorkFinished()
    {
    qDebug() << Q_FUNC_INFO;
    }

    void Dialog::on_pushButton_clicked()
    {
    emit doWork();
    }
    @

    This works fine so far.

    I want the onDoWork to be executed in a new thread so that the GUI is not blocked.

    I thought I can do this by doing a moveToThread and then connect object.

    But if I uncomment the moveToThread line the two signal/slot connections above doesn't work anymore.

    So my first question is:
    Why does they not work anymore?

    Second question is:
    Is this the right way to achieve that the onDoWork runs in a new thread?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      I don't see you calling "start":http://qt-project.org/doc/qt-4.8/qthread.html#start on the m_workerThread object. So this would mean the thread is never started. So no running eventloop.

      Another option to run the onDoWork() function in another thread would be to use "QtConcurrent":http://qt-project.org/doc/qt-4.8/qtconcurrentrun.html .

      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