Results 1 to 2 of 2

Thread: combining external lib callbacks with QT signals

  1. #1
    Join Date
    Jan 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default combining external lib callbacks with QT signals

    Hello,

    I'm trying to set up a GUI with QT using PortAudio to capture sound. For contious recording i use two buffer which I switch, when one is filled with Data. Now I want to use a new thread to process the full Buffer (save data). That's where the Problem lies... I have created that thread and tryed to interface it via a signal. But the signal is emitted from within the callback thread and not the main thread. For some reason this is not working.

    My savethread class:
    Qt Code:
    1. class SaveBuffer : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SaveBuffer(QObject *parent = 0);
    7. ~SaveBuffer();
    8.  
    9. void process();
    10.  
    11. protected:
    12. void run();
    13.  
    14. protected:
    15. bool finish;
    16.  
    17. private:
    18.  
    19. QMutex mutex;
    20. QWaitCondition condition;
    21. SDataStruct sData;
    22. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "SaveBuffer.h"
    2.  
    3. SaveBuffer::SaveBuffer(QObject *parent)
    4. : QThread(parent)
    5. {
    6. sData.file.setFileName("recorded.raw");
    7. sData.file.open(QIODevice::Append);
    8. }
    9.  
    10. SaveBuffer::~SaveBuffer()
    11. {
    12. //mutex.lock();
    13. condition.wakeOne();
    14. //mutex.unlock();
    15.  
    16. sData.file.close();
    17. wait();
    18. }
    19.  
    20. void SaveBuffer::run()
    21. {
    22. forever
    23. {
    24. QMutexLocker locker(&mutex);
    25.  
    26. QFile *file = &sData.file;
    27. int frameIndex = *sData.FrameIndex;
    28. SAMPLE *buffer = sData.buffer;
    29.  
    30. QDataStream out(file);
    31. out.setByteOrder(QDataStream::LittleEndian);
    32. for(int k=0;k<frameIndex;k++)
    33. {
    34. out << (qreal)buffer[k];
    35. }
    36. condition.wait(&mutex);//put thread to sleep
    37. }
    38. };
    39.  
    40. void SaveBuffer::process(DataStruct* incData)
    41. {
    42. QMutexLocker locker(&mutex);
    43.  
    44. if (!isRunning()) {
    45. start(LowPriority);
    46. } else {
    47. condition.wakeOne();
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    My window class:
    Qt Code:
    1. class EEGacquisition : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. EEGacquisition(QWidget *parent = 0, Qt::WFlags flags = 0);
    7. ~EEGacquisition();
    8. void processDataEmitter(void*);
    9.  
    10. private:
    11. Ui::EEGacquisitionClass ui;
    12. SaveBuffer sthread;
    13.  
    14.  
    15. signals:
    16. void processData();
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int recordCallback( const void *inputBuffer, void *outputBuffer,
    2. unsigned long framesPerBuffer,
    3. const PaStreamCallbackTimeInfo* timeInfo,
    4. PaStreamCallbackFlags statusFlags,
    5. void *userData,
    6. void *ui);
    7.  
    8. EEGacquisition::EEGacquisition(QWidget *parent, Qt::WFlags flags)
    9. : QMainWindow(parent, flags)
    10. {
    11. ui.setupUi(this);
    12.  
    13. //connects
    14. connect(ui.pushButton_measure,SIGNAL(clicked()),this,SLOT(pushButton_measure_clicked()),Qt::DirectConnection);
    15.  
    16. /*---portaudio setup---*/
    17. [...]
    18. }
    19. }
    20.  
    21.  
    22. void EEGacquisition::processDataEmitter(void *user_int)
    23. {
    24. // problem here if i emit a signal I am still in record callback
    25. // I cannot connet this signal
    26. }
    27.  
    28. /////
    29. //callback for recording audio data
    30. /////
    31. static int recordCallback( const void *inputBuffer, void *outputBuffer,
    32. unsigned long framesPerBuffer,
    33. const PaStreamCallbackTimeInfo* timeInfo,
    34. PaStreamCallbackFlags statusFlags,
    35. void *userData,
    36. void *user_int)
    37. {
    38. [...]
    39.  
    40. //at some point emit save signal!!!
    41. ui->processDataEmitter();
    42.  
    43. [...]
    44.  
    45. return paContinue;//0
    46. }
    To copy to clipboard, switch view to plain text mode 

    I also tried directly calling the process() function in sthread, but doing so led to an exeption, because the call wasnt from the main window thread.

    Can someone please explain some way to start the save thread?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: combining external lib callbacks with QT signals

    Don't subclass QThread. Instead create an instance of QThread and encapsulate whole functionality of your worker thread in a QObject subclass. Then use QObject::moveToThread() to move that object to the newly created thread. Alternatively don't use signals and slots. Instead of that it should be possible to use a wait condition to trigger the waiting thread (if I understood correctly what you wanted).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QScript script side callbacks
    By starlon in forum Qt Programming
    Replies: 0
    Last Post: 11th November 2009, 00:29
  2. Qt Plugins and Callbacks
    By abernat in forum Qt Programming
    Replies: 8
    Last Post: 25th February 2009, 00:51
  3. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 23:18
  4. Qt and Callbacks
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 17:08
  5. C callbacks and QT
    By taylor34 in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2006, 20:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.