[Qt-interest] bloody ungraspable control of my pusbutton

Ross Bencina rossb-lists at audiomulch.com
Tue Feb 2 08:35:08 CET 2010


Greetings Sal

The basic problem is that you shouldn't run a loop like that in any event driven application. You need to allow Qt to return to its event loop so it can process further UI events after you have processed the pressed() signal from the check box. The idea would be to set something running to generate the audio when you detect that the button has been checked, and then stop it when it has been unchecked -- in the meantime you return to Qt so it can do it's thing.

You have a few options:


1. Run your audio synthesis in a separate thread (perhaps with QThread). Start the thread when the checkbox is checked and then arrange for it to be stopped (via a flag or something) when the checkbox is unchecked. Note that you can't safely check the isChecked() flag from another thread.

2. Refactor your audio loop so it can be polled by a timer and use QTimer to periodically poll -- in the timer slot check whether any new audio needs to be generated.

3. Use an audio API which gives async. callbacks and do your audio processing there. ALSA can probably do that. You might also want to check out PortAudio (portaudio.com, it's free and crossplatform) which lets you write an audio generation callback and has StartStream and StopStream functions which you could easily call from your checkbox button signal handler.

4. Finally, if you really want to persist with your while loop, if you add the following lines of code within the loop, it should work:
    QApplication::sendPostedEvents();

    QApplication::processEvents();

    That would be the worst programming style but the quickest hack.

I recommend (2) or (3) since they are relatively foolproof compared to managing your own threads.



HTH

Ross.




  ----- Original Message ----- 
  From: S. Aguinaga 
  To: Qt Interest 
  Sent: Tuesday, February 02, 2010 3:21 PM
  Subject: [Qt-interest] bloody ungraspable control of my pusbutton


  Dear Fellows,


  I have a question for the best of you.


  I have a button that initiates a tone stimulus using my audio device.
  This stimulus is meant to be continuous until the pushbutton is toggled,
  but what is happening is that once my code goes into the audio loop it 
  never allows control back to my pushbutton, in other words, I can't toggle it
  and right now the only way out is to go to the terminal and do control-C.


  here is the loop:


  while ( contToneButton->isChecked() ) {
          chk_msg();
          c = (int) floor((1 + oct0) * ncue / 2);
          cue(c);
          (void) ar_io_cur_seg(dvid);
  };


  have any of you come across something like this an do you mind sharing 
  your methods?




  Thank you


  // Sal Aguinaga
  // Northwestern University






------------------------------------------------------------------------------


  _______________________________________________
  Qt-interest mailing list
  Qt-interest at trolltech.com
  http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100202/147cdd82/attachment.html 


More information about the Qt-interest-old mailing list