In a project I use Qthreads. Some actions are triggered by clicking a button, and are executed from a thread (to lete interfac be responsive during actions executions)
I have attached a little example to figure out my doubt
ThreadTest.zip
I have this dialog:
ThreadExample.jpg
When the button is clicked is called the following slot:
void Dialog::slotBtn(void)
{
// MyThread *myt; // declared in the class
myt = new MyThread;
myt->start();
}
void Dialog::slotBtn(void)
{
// MyThread *myt; // declared in the class
myt = new MyThread;
myt->start();
}
To copy to clipboard, switch view to plain text mode
where MyThread is:
#include <QThread>
{
public:
MyThread();
void run(void);
};
MyThread::MyThread()
{
}
void MyThread::run(void)
{
// some stuff that can takes some time
sleep(1);
qWarning("hallo world from MyThread");
//end of thread
}
#include <QThread>
class MyThread : public QThread
{
public:
MyThread();
void run(void);
};
MyThread::MyThread()
{
}
void MyThread::run(void)
{
// some stuff that can takes some time
sleep(1);
qWarning("hallo world from MyThread");
//end of thread
}
To copy to clipboard, switch view to plain text mode
I wonder when to delete myt.
For example, when a thread finish a signal QThread::finished() is emitted. I can connect to a slot, but I wonder if it is safe to delete it inside that slot.
thanks
Bookmarks