2

I have a worker class, that collects input from ncurses in an infinite loop, and emits a signal when a key is pressed. The worker is attached to a QThread as mentioned in the guidelines. This works fine, but the signal is never caught by the slot.

Header file:

#ifndef INPUT_H
#define INPUT_H

#include <QObject>
#include <QThread>

class InputWorker : public QObject {
    Q_OBJECT
public:
    InputWorker();
    ~InputWorker();
protected:
    bool running = true;

public slots:
    void run();
    void quit();

signals:
    void key(char ch);
};

class Input : public QObject {
    Q_OBJECT
public:
    Input();
public slots:
    void key(char ch);
};

#endif // INPUT_H

Source file:

#include "input.h"
#include "ncurses.h"
#include <QDebug>

InputWorker::InputWorker() {

}

InputWorker::~InputWorker() {

}

void InputWorker::run() {
    while (this->running) {
        char ch = getch();
        emit this->key(ch);
        thread()->sleep(0.3);
    }
}

void InputWorker::quit() {
    this->running = false;
}

Input::Input() : QObject() {
    InputWorker *w = new InputWorker();
    QThread *thread = new QThread();

    w->moveToThread(thread);

    connect(thread, SIGNAL(started()), w, SLOT(run()));
    connect(thread, SIGNAL(finished()), w, SLOT(quit()));
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    connect(w, SIGNAL(key(char)), this, SLOT(key(char)));
    thread->start();
}

void Input::key(char ch) {
    qDebug() << ch; // never happens
}
Erik
  • 2,137
  • 3
  • 25
  • 42
Shadow Prince
  • 198
  • 1
  • 1
  • 7
  • possible duplicate of [How to emit cross-thread signal in Qt?](http://stackoverflow.com/questions/638251/how-to-emit-cross-thread-signal-in-qt) – timday Dec 26 '13 at 19:18
  • Why do you inherit QObject rather than QThread or why do you include QThread in the header? That is not consistent. :) – László Papp Dec 27 '13 at 02:31
  • @timday: how is that a duplicate? The OP seems to be already doing what is mentioned in the other post. – László Papp Dec 27 '13 at 02:57
  • ShadowPrince: just in case, the control flow reaches the "emit this->key(ch);" line that you checked with a qDebug() print, for instance? – László Papp Dec 27 '13 at 02:59
  • QThread inheritation is deprecated for newer versions of Qt (becose qthread must contain implementation of threading only), so there is standart scheme from official docs. And, control flow reaches emit construction, I check this as you say. – Shadow Prince Dec 27 '13 at 06:20
  • Works fine here. But qDebug doesn't produce any output when `ch=0`. Maybe this is your case? Try to put `qDebug() << "ok";` in your slot. Also connecting `QThread::finished` to `InputWorker::quit` is useless. When the thread is finished, there is nothing to stop. And the thread will not finish until you call `InputWorker::quit` directly. – Pavel Strakhov Dec 27 '13 at 09:13
  • `getch()` returns an int, does it not? So put value in an int and debug-print the real value. Might be -1, is what I am suspecting. – hyde Mar 14 '14 at 19:02

0 Answers0