PDA

View Full Version : Problems with a QThread



franco.amato
4th April 2010, 09:37
Hi to all,
I would put in a separate thread the code to encode in an mp3 file some audio datas.


#include <QThread>

class WaveEncoder : public QThread
{
public:
WaveEncoder( QString& sndName, QString& outputDir, bool bg, SoundData*, WaveDisplay*, QObject* parent = 0 );
~WaveEncoder();

protected:
void run();

private:
int EncodeToMp3();
void ProcessTitle( QString&, QString& );
bool LoadBgSound();

SoundData* m_wave;
WaveDisplay* m_WaveDisplay;

QString m_SoundName;
QString m_OutputDir;

/* for the background audio */
signed short* inbuffer;
FMOD::Sound* bgSound;
unsigned int bgBytes; // length in Bytes
unsigned int bgSamples; // length in PCM
/* to call sound::lock() */
void* ptr1;
void* ptr2;
unsigned int len1;
unsigned int len2;

bool m_bgAudio;
};

#endif //__WAVEENCODER_H__

the ctor


WaveEncoder::WaveEncoder( QString& sndName,
QString& outputDir,
bool bg,
SoundData* sndData,
WaveDisplay* waveDisp,
QObject* parent )
: QThread(parent)
{
m_SoundName = sndName;
m_OutputDir = outputDir;
m_wave = sndData;
m_WaveDisplay = waveDisp;
}

and the run method


void WaveEncoder::run()
{
EncodeToMp3();
}

Here is where I create and run the thread:


void WaveWidget::cutSound()
{
WaveEncoder encoder(m_SoundName, m_outputDir, false, m_wave, m_WaveDisplay);
encoder.start();
}

I get this error:

ASSERT failure in QThread::setTerminationEnabled(): Current thread was not started with QThread....

What does mean? And how can I solve this error?

Regards,
Franco

borisbn
4th April 2010, 09:59
destructor of your class WaveEncoder is calling when you leave function WaveWidget::cutSound(). You need to do like this


void WaveWidget::cutSound()
{
m_encodeThread = new WaveEncoder(m_SoundName, m_outputDir, false, m_wave, m_WaveDisplay);
m_encodeThread->start();

franco.amato
5th April 2010, 06:03
destructor of your class WaveEncoder is calling when you leave function WaveWidget::cutSound(). You need to do like this


void WaveWidget::cutSound()
{
m_encodeThread = new WaveEncoder(m_SoundName, m_outputDir, false, m_wave, m_WaveDisplay);
m_encodeThread->start();



I did it and now I get this error:

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread"

I hope to get help to solve my problem

borisbn
5th April 2010, 06:18
try this in constructor of your thread:


moveToThread( this );

franco.amato
5th April 2010, 08:13
try this in constructor of your thread:


moveToThread( this );


I have another problem.
I would be notified when a thread finish

borisbn
5th April 2010, 08:19
ok, attach your code to your next reply in zip archive (widget's and thread's cpp- and h- files).
actualy working code in WaveEncoder::run() function doesn't need, if only it doesn't emitting signals to main widget

franco.amato
5th April 2010, 08:34
ok, attach your code to your next reply in zip archive (widget's and thread's cpp- and h- files).
actualy working code in WaveEncoder::run() function doesn't need, if only it doesn't emitting signals to main widget

Hi actually the run fuction doesn't emit any signal. It only call a routine.
How can I attach here a zip archive?

borisbn
5th April 2010, 08:48
click "Reply to Thread", then "Go Advanced", and click "Manage Attachments"

faldzip
5th April 2010, 09:00
make a slot in your class and connect QThread::finished() signal to it.

franco.amato
5th April 2010, 09:34
click "Reply to Thread", then "Go Advanced", and click "Manage Attachments"

Done! It doesn't accept zip attachments

borisbn
5th April 2010, 11:46
Done! It doesn't accept zip attachments
yes, after "Qt Centre has been moved to a new hosting" really doesn't work. and I can't see your initial code (only plain text). repeat your first message, please