简体   繁体   中英

(Qt5, Threads) The examples I'm following don't work

Sorry I'm not experienced enough to understand the error I'm getting and I have been trying now for weeks to understand what the problem is.

WHAT I NEED TO ACCOMPLISH:
Run multiple threads to control hardware and tests at the same time.

MY CODE:
I Followed the example provided here . This example is also available on many other sites so clearly it must work.

In my main code I have:

MotorClass *MotorObj = new MotorClass;
QThread *MotorThread = new QThread;

MotorObj->moveToThread(MotorThread);

connect(MotorThread, SIGNAL(started()), MotorObj, SLOT(RunMotor()));
connect(MotorObj, SIGNAL(finished()), MotorThread, SLOT(quit()));
connect(MotorObj, SIGNAL(finished()), MotorObj, SLOT(deleteLater()));
connect(MotorThread, SIGNAL(finished()), MotorThread, SLOT(deleteLater()));

MotorThread->start();

In my MotorClass.h I have:

class MotorClass : public QObject
{
    Q_OBJECT
public:
    explicit MotorClass(QObject *parent = 0);
    ~MotorClass();

public slots:
    void RunMotor();

signals:
    void finished();

};

In my MotorClass.cpp I have:

MotorClass::MotorClass(QObject *parent) : QObject(parent)
{
}

MotorClass::~MotorClass()
{
}

void MotorClass::RunMotor()
{
    qDebug("running");
    emit finished();
}


MY PROBLEM:
qDebug() does display the “running” message, but then it is followed by the following output and the program crashes.

QWinEventNotifier: event notifiers cannot be disabled from another thread
QWinEventNotifier: event notifiers cannot be disabled from another thread
QWinEventNotifier: event notifiers cannot be disabled from another thread
The program has unexpectedly finished.


WHAT I HAVE TRIED SO FAR:
If I comment out the connect code that is suppose to close the MotorThread and MotorObj , the errors disappear but obviously after running the program for long periods at a time causes it to crash. I understand this is because the MotorObj and MotorThread aren't being closed. I have tried other methods like inheriting the QThread class and then re-implementing the run() function but this does not cover my requirements and is also not the recommended way of using QThread s.

Any Ideas where I'm going wrong???

connect(MotorThread, SIGNAL(started()), MotorObj, SLOT(RunMotor()));
connect(MotorObj, SIGNAL(finished()), MotorThread, SLOT(quit()));
connect(MotorObj, SIGNAL(finished()), MotorObj, SLOT(deleteLater()));
connect(MotorThread, SIGNAL(finished()), MotorThread, SLOT(deleteLater()));

Looks like you are deleting the MotorThread after first execution has finished. So on next MotorThread->start() the program should crash, since it's only a dangling pointer. However, even if you remove the last connect, also the MotorObj does not exist anymore, so the slot might only be called once.

Why dont you call the deleteLater() right after the objects aren't needed anymore!?

Ok. So after another week of sitting and trying to understand this weird problem, and I found a solution. Yet I don't understand why the problem occurred. I created new header and .cpp files for MotorClass and copied all the code from the original files to the new ones. Problem gone. Now everything works fine. Does not make sense at all though. But thanks for all your help guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM