与QThread的通信不使用信号/插槽

时间:2014-05-08 21:32:06

标签: c++ qt signals-slots qthread qobject

我遵循了QThreads的本教程: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

我遇到的唯一问题是杀死QThread。我在工作者对象的process()循环中有一个标志,我将其推入名为QThread的{​​{1}}实例中。如果该标志被翻转为假,那么我的处理循环中断并且工作者对象发出m_ShouldSendFrames信号。

我遇到的问题是我似乎无法在finished()中获取我的worker对象以从主线程接收QThread信号。我连接(并且连接返回' true'),如下所示:

stopSendingFrames()

在我的connect(this, SIGNAL(stopSendingFrames()), m_UdpWorkerBlended, SLOT(stopFrames()),Qt::QueuedConnection); // Stop broadcasting 函数中,我想发出killUdpThread信号并让stopSendingFrames()对象获取它。但它永远不会!我可以改变标志的唯一方法是在m_UdpWorker

中执行此操作
killUdpThread

然而,这是不安全的并且随机崩溃,因为它没有从工作人员正在运行的m_UdpWorkerBlended->stopFrames(); 中调用。

我也尝试过QThread方式 - 但它也不会触发插槽:

invokeMethod

有人可以帮助我理解为什么我不能拨打QMetaObject::invokeMethod(m_UdpWorkerBlended,"stopFrames",Qt::QueuedConnection); 或使用信号来激活我在invokeMethod的工作对象中运行的插槽,但我可以直接调用它(不安全)吗?谢谢!

启动线程功能:

QThread

杀死线程功能:

int UdpThreadController::startUdpThread(int frameType, int port, QString ip) {

    #if SINGLETON_CAMERA_UDP_DEBUG
    qDebug() << "Starting Thread! Frame type is: " << frameType;
    #endif

    // Check for type of frame to emit
    if ( frameType == 0 ) {

        #if SINGLETON_CAMERA_UDP_DEBUG
        qDebug() << "Frames are vl";
        #endif

        // Delete any existing threads / workers
        killUdpThread(0);

        // New objects
        m_UdpThreadBlended = new QThread();
        m_UdpWorkerBlended = new UdpWorker();

        // Assign Port and IP and Frame Type
        m_UdpWorkerBlended->m_ShouldSendFrames = true;
        m_UdpWorkerBlended->m_Port = port;
        m_UdpWorkerBlended->m_AddressToUse = ip;
        m_UdpWorkerBlended->m_FrameType = frameType;

        // Push into thread
        m_UdpWorkerBlended->moveToThread(m_UdpThreadBlended);

        // Connect signals
        connect(this, SIGNAL(stopSendingFrames()), m_UdpWorkerBlended, SLOT(stopFrames()),Qt::QueuedConnection); // Stop broadcasting
        connect(m_UdpThreadBlended, SIGNAL(started()), m_UdpWorkerBlended, SLOT(process()));
        connect(m_UdpWorkerBlended, SIGNAL(finished()), m_UdpThreadBlended, SLOT(quit()));
        connect(m_UdpWorkerBlended, SIGNAL(finished()), m_UdpWorkerBlended, SLOT(deleteLater()));
        connect(m_UdpThreadBlended, SIGNAL(finished()), m_UdpThreadBlended, SLOT(deleteLater()));
        m_UdpThreadBlended->start();

        // All done
        return 0;

1 个答案:

答案 0 :(得分:0)

感谢您的投入。我通过一个单例来解决它,它有一个标志,QThread中我的worker对象中的处理循环读取每个循环迭代。它受QReadWrite锁保护。

int UdpThreadController::killUdpThread(int frameType) {

    #if SINGLETON_CAMERA_UDP_DEBUG
    qDebug() << "Killing Thread! Frame type is: " << frameType;
    #endif

    // Check for type of frame to emit
    if ( frameType == 0 ) {

        // Delete any existing threads / workers
        if ( m_UdpWorkerBlended ) {

            #if SINGLETON_CAMERA_UDP_DEBUG
            qDebug() << "Setting flag to kill thread...";
            #endif

            // Stop broadcasting
            m_Lock.lockForWrite();
            m_ShouldSendFramesBlended = false;
            m_Lock.unlock();

        }

        #if SINGLETON_CAMERA_UDP_DEBUG
        qDebug() << "Success ending Blended UDP...";
        #endif

        // All done
        return 0;

bool UdpThreadController::shouldContinueSendingFrames(int frameType) {

    #if SINGLETON_CAMERA_UDP_DEBUG
    qDebug() << "Checking if we should continue processing: " << frameType;
    #endif

    // Check for type of frame to emit
    if ( frameType == 0 ) {

        m_Lock.lockForRead();
        #if SINGLETON_CAMERA_UDP_DEBUG
        qDebug() << "Should continue Blended: " << m_ShouldSendFramesBlended;
        #endif
        bool shouldBroadcast = m_ShouldSendFramesBlended;
        m_Lock.unlock();

        // All done
        return shouldBroadcast;

    }