简体   繁体   中英

Running Threads Simultanously using Qt

I have a problem. I have a Qiwidget which has 2 pushbuttons. On pressing one button i need to play back 1 file using some playback technique. On clicking the other button I want to playback another file. I made the classes using the playback's for the 2 files as threads. But when i try to push the 1st button my application gets stuck and i am not able to press the second button. It gets blocked till my playback is over.

I want to be able to use my main application irrespective of the files playing. How can i achieve that in Qt.

playback file 1.h..

class PlaySource1 : public QThread
{
public:
    PlaySource1();
    virtual void run();
};

playbackfile.cpp

PlaySource1::PlaySource1()
{
}
void PlaySource1::run()
{
some code
}

now in my main file .cpp when i run the code like :

void Test::on_pbPlaySource1_clicked()
{
    PlaySource1 *playSource1 = new PlaySource1;
    playSource1->run();

}

my code gets blocked by the thread playback. But i dont want it to get blocked. Please help.

You should call

playSource1->start();

not run() . See the "Starting a Thread" section in the Qt Starting Threads with QThread documentation.

Try to add exec() to the end of run() function and take a look to the sbj of moveToThread() there Qt moveToThread() vs calling new thread when do we use each . Probably it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM