Pregunta

I have a main Qt Application and I am developing a Qt Library that I link to from this Main Application. From the Main Application I wish to call a function in the library that will do something, and then at the end of the routine call a QTimer which will fire off a Slot in the library code after some delay. I cannot get the timer to fire and I am not sure why. If I place a timer in my Main App, then it fires as expected, just not in the library.

For now, my library is just one class. In my library header file I define the slot I wish to call as:

private slots:

        void stop();

In the implementation file I have this:

    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;
    }

What am I missing here for the timer to fire?

NOTE: Here is alot of my header file - everything after this in the real file is just function calls:

/// 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();

            /// /// ///
}
¿Fue útil?

Solución

Well the following code works. I put 3 seconds in the start() function and now it fires off from the library call.

    // 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

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top