The Puma
The Puma

Reputation: 1390

What is the structure of a simple threaded gui application using Qt?

I'm trying to wrap my head around threading and gui applications so I can start to build them. I downloaded the Qt SDK but am a bit lost.

Just so I can understand how this works, I want to create a gui application that consists of a text box and two buttons. Each button would start a new thread (so 3 could be running at the same time: the main gui thread, and the 2 initiated from the buttons), and each would have access to the information in the text box. How would I set this application up?

Upvotes: 1

Views: 624

Answers (2)

SaKabmar_Ashna
SaKabmar_Ashna

Reputation: 144

You have to connect a slot to the QPushButton clicked signal and kickstart a new thread from that slot as shown below.

QPushButton *button=new QPushButton();

connect(button,SIGNAL(clicked()),this,SLOT(startthread()));

And the slot startthread will kickstart the the new thread with a worker class

void MyClass::startthread()
  {
       QThread *thread=new QThread();
       WorkerClass * worker=new WorkerClass();
       worker->moveToThread(thread);
       thread->start()
  }

Where WorkerClass will be executed in a separate thread.

Upvotes: 1

opc0de
opc0de

Reputation: 11768

Take a look at SIGNAL and SLOTS it's the base of Qt .

After you done that take a look at QThread and QThreadPool.

Also i recommend these awsome tutorials : http://www.voidrealms.com/tutorials.aspx?filter=qt

Upvotes: 2

Related Questions