我有一个数据模型,我希望其他对象能够监视更新,但我不想让任何人控制更新信号本身。我想出了一个在概念上对我有意义的东西,但它似乎不起作用。我想知道是否有人可以解释为什么我永远不会让它工作,或者我是否错过了可以使它工作的东西。实际上,我有一个具有任意插槽的客户端类(QObject)和具有私有信号的模型类。
重要的客户端类代码(公共SLOT):
void client::doUpdate()
{
std::cout << "HELLO UPDATE" <<std::endl;
}
型号代码:
void Model::unRegisterForUpdates(const char* qt_slot_handle, QObject* o)
{
QObject::disconnect (this, SIGNAL( updateHandles() ),
o, qt_slot_handle);
}
void Model::registerForUpdates(const char* qt_slot_handle, QObject* o)
{
QObject::connect( this, SIGNAL( updateHandles() )
, o, qt_slot_handle
, Qt::UniqueConnection);
}
主要功能:
Model foo;
client * cl = new client();
client * cl2 = new client();
std::cout << SLOT(cl->doUpdate()) << std::endl;
std::cout << SLOT(cl2->doUpdate()) << std::endl;
foo.registerForUpdates( SLOT(cl->doUpdate()) , cl);
foo.registerForUpdates( SLOT(cl2->doUpdate()) , cl2);
输出:
1cl->doUpdate()
1cl2->doUpdate()
Object::connect: No such slot client::cl->doUpdate() in .../main.cpp:14
Object::connect: No such slot client::cl2->doUpdate() in .../main.cpp:15
这可能会归结为我可以进入信号/插槽系统的自省量。我不确定如何解释连接错误消息。它告诉我 connect 与 Client 类的静态信息有关,但插槽字符串表示特定的实例名称 - 我想知道当我到达 Model::connectHandle() 时,这个名称是否失去了意义。