QTimer Slot没有被库函数触发

时间:2013-03-14 17:15:44

标签: qt shared-libraries signals-slots qtimer

我有一个主Qt应用程序,我正在开发一个Qt库,我从这个主应用程序链接到。从主应用程序我希望调用库中的一个函数来执行某些操作,然后在例程的末尾调用一个QTimer,它将在一段延迟后触发库代码中的Slot。我不能让计时器开火,我不知道为什么。如果我在我的主应用程序中放置一个计时器,它会按预期启动,而不是在库中。

现在,我的图书馆只是一个班级。在我的库头文件中,我将要调用的插槽定义为:

private slots:

        void stop();

在实现文件中我有:

    void MyLib::start() {

        // Create a timer to user during audio operation
        m_GeneralTimer = new QTimer(this);

        // Fire off a oneshot to clear the buffer for fluke-media
        m_GeneralTimer->setInterval(3000);
        m_GeneralTimer->setSingleShot(true);
        connect(m_GeneralTimer, SIGNAL(timeout()), SLOT(stop()));
        m_GeneralTimer->start();
    }
    void MyLib::stop() {

        qDebug() << "Called stop()...";
        m_GeneralTimer->stop();
        delete m_GeneralTimer;
    }

我在这里错过了什么让计时器开火?

注意:这里有很多我的头文件 - 真实文件中的所有内容都只是函数调用:

/// Use shared memory
#include <QSharedMemory>

/// Normal Qt Includes
#include <QBuffer>
#include <QDebug>

/// QTimer is required for calling a method
/// to stop audio playback at a later time
#include <QTimer>

/// Put into a background thread
#include <QtConcurrentRun>

/// Check integrity of received data
#include <QCryptographicHash>

class MYAUDIOLIBSHARED_EXPORT MyLib: public QObject
{

    Q_OBJECT

    public:

            /// /// ///

    private slots:

            void stop();

            /// /// ///
}

1 个答案:

答案 0 :(得分:1)

以下代码有效。我在start()函数中放了3秒钟,现在它从库调用中激活。

    // Create a timer to fire a slot when the playback is done
    m_EndPlayBackTimer = new QTimer(this);
    m_EndPlayBackTimer->setInterval(3000);
    m_EndPlayBackTimer->setSingleShot(true);
    connect(m_EndPlayBackTimer, SIGNAL(timeout()), SLOT(playBackDone()));
    m_EndPlayBackTimer->start(3000);

    // Done
    return;

}

/**
 * @brief
 * Slot to be started by a timer after some time delay
 * to signify that the playback is complete.
 *
 */
void MyLib::playBackDone() {

    #if DEBUG
    qDebug() << "Playback is complete...";
    #endif

}