PDA

View Full Version : Multi-threading



plopes21
27th December 2012, 10:37
Hello guys,

I come here to know about your opinion. I want have 2 threads running in my programm.

Threads:

GUI
Logical code


Main.h
start running the 2 threads

Mainwindow
using the 2 threads

Others dialogs (UI)
using the 2 threads

Is it possible right?

What best method to using this? QT Concurrent, QThreads ... ?

Thanks
Best Regards,Pedro Lopes

wysota
27th December 2012, 10:41
It really depends what you need those threads for.

plopes21
27th December 2012, 10:53
Thanks for your quick reply.

I would like something as...

1 programm and 2 threads. One thread to GUI and other for worker. The GUI speaks with the worker for her to do something.

For example...

in my mainwindow i have 1 button ("Count to 10000").this button calls the logical thread (logical class) and count to 10000. And simultaneously is showing wait dialog.

amleto
27th December 2012, 15:11
yes, this is possible. You should consider QThread and qt concurrent according to your needs.

wysota
27th December 2012, 21:53
1 programm and 2 threads. One thread to GUI and other for worker. The GUI speaks with the worker for her to do something.
This still doesn't determine what the worker thread is used for.


in my mainwindow i have 1 button ("Count to 10000").this button calls the logical thread (logical class) and count to 10000. And simultaneously is showing wait dialog.
Well... for that you don't need a worker thread.

plopes21
2nd January 2013, 16:01
In my mainwindow i have



th = new QThread;
qDebug()<< "ID->" << thread()->currentThreadId();
worker= new Worker(&ClassHasSomethingToDo);
worker->moveToThread(th);
th->start();


So, as I have above I can get two threads running in simultaneous.

Mainwindow has a button when is clicked, shows a dialog.

This dialog has to communicate with the class(ClassHasSomethingToDo) passed the Worker.

In dialog I have



QInitTokenDialog(ClassHasSomethingToDo*todo, QWidget *parent)
this->todo=todo;
connect(todo, SIGNAL(message(QString)), ui->textEdit, SLOT(setText(QString)));
QMetaObject::invokeMethod(todo, "getMessage", Qt::AutoConnection);


I cant catch the signal emitted by ClassHasSomethingToDo. Why guys?

Thanks for all answers, and sorry my English :P

wysota
2nd January 2013, 17:45
How are we supposed to know that? Does your connect() statement return true or false? Do you get any warnings on the console while the program is running?