1

我有一个数据模型,我希望其他对象能够监视更新,但我不想让任何人控制更新信号本身。我想出了一个在概念上对我有意义的东西,但它似乎不起作用。我想知道是否有人可以解释为什么我永远不会让它工作,或者我是否错过了可以使它工作的东西。实际上,我有一个具有任意插槽的客户端类(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() 时,这个名称是否失去了意义。

4

1 回答 1

3

这是一个简单的错字案例:

在课堂上,你有doUpdate()插槽。

在 main func 中,您将传递onUpdate()SLOT()宏。

此外,您不应将实例包含在SLOT()宏中,而应仅包含插槽名称(和参数)。与您在connect(). Qt 的信号槽连接机制是基于字符串比较的。换句话说,你的 main 应该这样做:

foo.registerForUpdates(SLOT(doUpdate()), cl);
foo.registerForUpdates(SLOT(doUpdate()), cl2);
于 2013-05-10T17:20:57.767 回答