我有一个具有这种结构的应用程序:所有数据类型 ( class INode
) 都存储在插件 (DLL) 中。可以绘制一些数据类型(如果它们是IDrawable
)。
要加载一个对象,例如,class PointCloudNode: public INode
我有一个特殊的输入插件(DLL),它被调用class PointCloudParser: public IIOPlugin
并且IIOPlugin
是一个具有某些特定功能的线程:class IIOPlugin: public QThread
.
所有对象均由NodeFactory
类创建,该类是存储在单独 DLL 中的单例。
这就是问题所在:
void PointCloudNode::update()
{
QObject::connect (this,SIGNAL(tmptmp()),this,SLOT(drawObject()));
emit tmptmp();
}
如果我从任何线程(主线程或输入插件线程)执行此操作
NodeFactory* fab = NodeFactory::getInstance();
boost::shared_ptr<INode> pc(fab->createNode("pointCloud","myPC"));
boost::shared_ptr<IDrawable> dr = boost::dynamic_pointer_cast<IDrawable>(pc);
dr->update();
更新启动,tmptmp()
发出信号,插槽 ( drawObject()
) 正确执行。
但是
如果做同样的事情,但是在我的输入插件中创建对象,传递共享指针并在另一个函数中执行,尽管所有代码都被执行(包括等),但永远不会进入dr->update()
插槽。drawObject()
connect
更准确地说,这里是输入插件:
void PointCloudParserPlugin::doLoad(const QString& inputName, boost::shared_ptr<INode> container)
{
NodeFactory* factory = NodeFactory::getInstance();
boost::shared_ptr<INode> node = factory->createNode("pointCloud", inputName);
// here goes the loading itself, nothing special...
container->addChild(node); //that's the container where I keep all the objects
//boost::dynamic_pointer_cast<IDrawable>(container->getChild(inputName))->update();
//If I uncomment this line, it all works: the slot is launched.
emit loadingFinished(inputName); // it executes the following function
}
最后一个发射连接到这个:
void GeomBox::updateVisualization(const QString& fileName)
{
boost::shared_ptr<INode> node = container_->getChild(fileName);
boost::shared_ptr<IDrawable> nodeDrawable = boost::dynamic_pointer_cast<IDrawable>(node);
nodeDrawable->update(); //this is the problem line: update() executes, connect() works, but the slot never runs :(
}
怎么来的?node
对象自始至终都是一样的,它是有效的。启动代码中的每一行,QObject::connect
都不向调试窗口写入任何内容,tmptmp()
发出信号,但drawObject()
在一种情况下从未到达插槽?有任何想法吗?
更新。:如果我不继承IIOPlugin
自QThread
,一切正常(即在主线程中加载对象)。我希望信号/插槽能够跨线程工作......