[Qt-interest] bloody ungraspable control of my pusbutton
S. Aguinaga
sa_jr at yahoo.com
Mon Feb 8 22:36:31 CET 2010
Hello!
Got audio working off a second thread and I am able to 'stop' it while it isRunning(),
but am I halting it gracefully, and what do I have to do to quit the app while thread is running?
A pushbutton is used to start the audio thread, then it is pressed again to Stop it.
connect(newstartButton, SIGNAL(clicked()), this, SLOT(startAudioThread()));
>connect(quitButton , SIGNAL(clicked()), qApp, SLOT(quit())); // This doesn't work cleanly
The slot: I use t_stop bool flag to check if the button was pressed to stop the the thread.
If the thread is started, I change the txt of the button to read "Stop Thread"
void AudKeyWidget::startAudioThread()
>{
>if (t_stop) {
>printf("Thread is running\n");
>stopAudio();
>}
>else {
>thread = new cAudioThread(this);
>thread->start();
>t_stop = !t_stop;
>if(t_stop)
>newstartButton->setText("Stop Thread");
>}
>}
If we are stopping the thread, then I handle this using function stopAudio().
void AudKeyWidget::stopAudio()
>{printf("Stop Audio\n");
>if (t_stop && thread->isRunning()) {
>thread->terminate();
>delete thread;
>t_stop = !t_stop;
>}
>}
But using terminate() offers the proper way to halt the thread? & reset things?
now my Continuous audio thread (caudiothread), some of this code was taken from the
mandelbrot example.
cAudioThread::cAudioThread(QObject *parent)
> : QThread(parent)
>{
> printf("-- cAudioThread(QObject *parent) : QThead(parent) ...\n");
> restart = false;
> abort = false;
>
>}
>
>cAudioThread::~cAudioThread()
>{
> mutex.lock();
> abort = true;
> condition.wakeOne();
> mutex.unlock();
>
> wait(); // Does is make sense to use this if we will almost
>// always halt the thread rather wait for it to end?
>
>}
>void cAudioThread::run()
>{
>printf("-- Independent thread, run() function started\n");
>forever {
>// ++++++++++++++++++ continuous audio (tone) ++++++++++++++++++ //
>blah, blah, blah
>// --------------------------- end continuous audio
>mutex.lock();
>if (!restart)
>condition.wait(&mutex);
>restart = false;
>mutex.unlock();
>}// ends forever
>
>}
Finally the caudiothread.h, I would be interested in how to quit while running and if stopAudio offers a clean approach to halt the thread.
class cAudioThread : public QThread
>{
> Q_OBJECT
>
>>
>public:
> cAudioThread(QObject *parent = 0);
> ~cAudioThread();
>
>>
> void playAudio( int );
>
>>
>protected:
> void run();
>
>>
>private:
>
>>
> QMutex mutex;
> QWaitCondition condition;
> bool restart;
> bool abort;
>
>>
>};
>
>>
>
>>// Salvador Aguinaga
// Northwestern Univeristy
>
________________________________
From: S. Aguinaga <sa_jr at yahoo.com>
To: Qt Interest <Qt-interest at trolltech.com>
Sent: Tue, February 2, 2010 7:15:52 AM
Subject: Re: [Qt-interest] bloody ungraspable control of my pusbutton
Thank you Ross, Amulya, and Jeroen for your responses and suggestions.
I will work on this and report back and post my solution ( for others ).
// Sal
________________________________
From: S. Aguinaga <sa_jr at yahoo.com>
To: Qt Interest <Qt-interest at trolltech.com>
Sent: Mon, February 1, 2010 10:31:04 PM
Subject: Re: [Qt-interest] bloody ungraspable control of my pusbutton
I left out that that my application is a linux application an I am running
qt 4.6
The audio access is based on alsa, I'm wondering why this method becomes
so strong and focused that even trying to quit the app does not responds.
Does this pieces of code need an interrupt of some type? What other instances
cause such a similar behavior?
I'm sure that there is some method that can address this issue. If you want me
to post the entire qt code, I can.
Thanks in advance.
// Sal
________________________________
From: S. Aguinaga <sa_jr at yahoo.com>
To: Qt Interest <Qt-interest at trolltech.com>
Sent: Mon, February 1, 2010 10:21:56 PM
Subject: 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100208/d9553866/attachment.html
More information about the Qt-interest-old
mailing list