QRunnable在完成后被QThreadPool销毁。当我从它发出信号并尝试使用sender()
从插槽获取QRunnable对象时,它是NULL。QRunnable发出信号并从插槽获取发件人
小例子:
// class MyRunnable : public QObject, public QRunnable
MyRunnable::run()
{
//... do some work
emit onFinished();
}
// constructor by request
MyRunnable::MyRunnable(QObject *parent) : QObject(parent),
m_someData(1),
{
}
...
private slots:
void onFinished()
{
MyRunnable* myRunnable = qobject_cast<MyRunnable*>(sender());
int val = myRunnable->getSomething(); // myRunnable is null and it crashes
}
...
// later I start it using some thread pool
MyRunnable* myRunnable = new MyRunnable;
connect(myRunnable, SIGNAL(onFinished()), this, SLOT(onFinished());
threadPool.start(myRunnable);
有没有什么办法可以指定何时删除此对象吗?所以我可以安全地访问我的插槽中的数据成员?
显示MyRunnable类的构造函数,请 – eyllanesc