2

我创建了我自己的TestService,它在单独QThread的. 我看到了一个类似的问题,但是那里的问题略有不同,因为 OP 超载,而我只是将我的课程移到了线程中。MainLoopQThread::finishedQThread

请注意,我没有重载QThread类,我只QObject根据这个例子重载:http: //mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation /

这是我的TestService课:

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <iostream>

using namespace std;
class TestService: public QObject
{
    Q_OBJECT;
private:
    volatile int _count;
    QWaitCondition _monitor;
    QMutex _mutex;
    QThread* _thread;

public:
    TestService(int numSeconds)
    {
        _count = numSeconds;
        _thread = NULL;
        cout << "TestService()" << endl;
    }

    virtual ~TestService()
    {
        cout << "~TestService()" << endl;
    }

    void Start()
    {
        QMutexLocker locker(&_mutex);
        if(_thread == NULL)
        {
            _thread = new QThread;

            // Move this service to a new thread
            this->moveToThread(_thread);

            // The main loop will be executed when the thread
            // signals that it has started.
            connect(_thread, SIGNAL(started()), this, SLOT(MainLoop()));

            // Make sure that we notify ourselves when the thread
            // is finished in order to correctly clean-up the thread.
            connect(_thread, SIGNAL(finished()), this, SLOT(OnFinished()));

            // The thread will quit when the sercives
            // signals that it's finished.
            connect(this, SIGNAL(Finished()), _thread, SLOT(quit()));

            // The thread will be scheduled for deletion when the 
            // service signals that it's finished
            connect(this, SIGNAL(Finished()), _thread, SLOT(deleteLater()));

            // Start the thread
            _thread->start();
        }
    }

    void Stop()
    {
        _count = 0;
        _monitor.wakeAll();
    }
private slots:
    void MainLoop()
    {
        cout << "MainLoop() Entered" << endl;
        while(_count > 0)
        {
            cout << "T minus " << _count << " seconds." << endl;

            QMutexLocker locker(&_mutex);
            _monitor.wait(&_mutex, 1000);
            _count--;
        }
        cout << "MainLoop() Finished" << endl;
        emit Finished();
    }

    virtual void OnFinished()
    {
        cout << "OnFinished()" << endl;
    }
signals:
    void Finished();
};

这是测试代码:

void ServiceTest()
{
    cout << "Press q to quit." << endl;
    cout << "Press s to start." << endl;
    cout << "Press t to stop." << endl;
    QSharedPointer<TestService> testService(new TestService(10));
    char in = 'a';
    while( in != 'q' )
    {
        switch(tolower(in))
        {
        case 's':
            testService->Start();
            break;
        case 't':
            testService->Stop();
            break;
        default:
            break;
        }
        cin.get(in);
        in = tolower(in);
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ServiceTest();

    QTimer::singleShot(0, &a, SLOT(quit()));

    return a.exec();
}

输出是:

Press q to quit.
Press s to start.
Press t to stop.
TestService()
s
MainLoop() Entered
T minus 10 seconds.
T minus 9 seconds.
T minus 8 seconds.
t
MainLoop() Finished
q
~TestService()
Press any key to continue . . .

任何人都可以解释为什么finished没有被发射我该如何解决它?

4

1 回答 1

8

信号已完成()因原因而发出,但您没有抓住它。

这里:

connect(_thread, SIGNAL(finished()), this, SLOT(OnFinished()));

Qt::QueuedConnection被使用,因为_threadthis(service) 在不同的线程中。

finished()发出的时候,_thread的事件循环已经完成执行,所以信号不会被传递到插槽。

您可以显式使用Qt::DirectConnection.

编辑:

QTherad 的工作原理是这样的:

QThread::start()
{
    emit started();
    run();
    emit finished();
}

QThread::run()
{
     eventloop->exec();
}

因此,在finished发出时,eventloop 已经停止执行。当你移动service到 时_thread,服务的事件循环就是_thread事件循环。

请注意,它QObject本身没有自己的事件循环。事件循环由对话框、线程和应用程序创建。


实际上,我会建议在您的简单情况下只使用QtConcurent::run,因为您不在新线程中执行实际的事件处理,而只是运行单个函数。

于 2012-11-19T18:56:42.023 回答