Results 1 to 11 of 11

Thread: Problems with a QThread

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with a QThread

    Hi to all,
    I would put in a separate thread the code to encode in an mp3 file some audio datas.

    Qt Code:
    1. #include <QThread>
    2.  
    3. class WaveEncoder : public QThread
    4. {
    5. public:
    6. WaveEncoder( QString& sndName, QString& outputDir, bool bg, SoundData*, WaveDisplay*, QObject* parent = 0 );
    7. ~WaveEncoder();
    8.  
    9. protected:
    10. void run();
    11.  
    12. private:
    13. int EncodeToMp3();
    14. void ProcessTitle( QString&, QString& );
    15. bool LoadBgSound();
    16.  
    17. SoundData* m_wave;
    18. WaveDisplay* m_WaveDisplay;
    19.  
    20. QString m_SoundName;
    21. QString m_OutputDir;
    22.  
    23. /* for the background audio */
    24. signed short* inbuffer;
    25. FMOD::Sound* bgSound;
    26. unsigned int bgBytes; // length in Bytes
    27. unsigned int bgSamples; // length in PCM
    28. /* to call sound::lock() */
    29. void* ptr1;
    30. void* ptr2;
    31. unsigned int len1;
    32. unsigned int len2;
    33.  
    34. bool m_bgAudio;
    35. };
    36.  
    37. #endif //__WAVEENCODER_H__
    To copy to clipboard, switch view to plain text mode 

    the ctor
    Qt Code:
    1. WaveEncoder::WaveEncoder( QString& sndName,
    2. QString& outputDir,
    3. bool bg,
    4. SoundData* sndData,
    5. WaveDisplay* waveDisp,
    6. QObject* parent )
    7. : QThread(parent)
    8. {
    9. m_SoundName = sndName;
    10. m_OutputDir = outputDir;
    11. m_wave = sndData;
    12. m_WaveDisplay = waveDisp;
    13. }
    To copy to clipboard, switch view to plain text mode 

    and the run method

    Qt Code:
    1. void WaveEncoder::run()
    2. {
    3. EncodeToMp3();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Here is where I create and run the thread:

    Qt Code:
    1. void WaveWidget::cutSound()
    2. {
    3. WaveEncoder encoder(m_SoundName, m_outputDir, false, m_wave, m_WaveDisplay);
    4. encoder.start();
    5. }
    To copy to clipboard, switch view to plain text mode 

    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
    Franco Amato

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with a QThread

    destructor of your class WaveEncoder is calling when you leave function WaveWidget::cutSound(). You need to do like this
    Qt Code:
    1. void WaveWidget::cutSound()
    2. {
    3. m_encodeThread = new WaveEncoder(m_SoundName, m_outputDir, false, m_wave, m_WaveDisplay);
    4. m_encodeThread->start();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with a QThread

    Quote Originally Posted by borisbn View Post
    destructor of your class WaveEncoder is calling when you leave function WaveWidget::cutSound(). You need to do like this
    Qt Code:
    1. void WaveWidget::cutSound()
    2. {
    3. m_encodeThread = new WaveEncoder(m_SoundName, m_outputDir, false, m_wave, m_WaveDisplay);
    4. m_encodeThread->start();
    To copy to clipboard, switch view to plain text mode 
    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
    Franco Amato

  4. #4
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with a QThread

    try this in constructor of your thread:
    Qt Code:
    1. moveToThread( this );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with a QThread

    Quote Originally Posted by borisbn View Post
    try this in constructor of your thread:
    Qt Code:
    1. moveToThread( this );
    To copy to clipboard, switch view to plain text mode 
    I have another problem.
    I would be notified when a thread finish
    Franco Amato

  6. #6
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with a QThread

    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

  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with a QThread

    Quote Originally Posted by borisbn View Post
    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?
    Franco Amato

  8. #8
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with a QThread

    click "Reply to Thread", then "Go Advanced", and click "Manage Attachments"

  9. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems with a QThread

    make a slot in your class and connect QThread::finished() signal to it.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  10. #10
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with a QThread

    Quote Originally Posted by borisbn View Post
    click "Reply to Thread", then "Go Advanced", and click "Manage Attachments"
    Done! It doesn't accept zip attachments
    Franco Amato

  11. #11
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with a QThread

    Quote Originally Posted by franco.amato View Post
    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

Similar Threads

  1. Problems with Qsemaphore and QThread
    By perseo in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 02:21
  2. Replies: 4
    Last Post: 26th June 2008, 19:41
  3. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 13:54
  4. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 09:25
  5. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 16:39

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.