简体   繁体   中英

Private QT signal

I have a data model that I want other objects to be able to watch for updates, but I don't want to give anyone control of the update signal itself. I've come up with a something that makes sense to me conceptually, but it doesn't seem to work. I'm wondering if anyone could explain why I will never get it to work, or if i'm missing something that could make this work. Effectively I have a Client class (QObject) that has an arbitrary slot and Model class that has a private signal.

important Client class code (public SLOT):

void client::doUpdate()
{
  std::cout << "HELLO UPDATE" <<std::endl;
}

Model code:

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);  
}

Main func:

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);

Output:

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

It will probably come down to the amount of introspection I can get into the signal/slot system.I'm not sure how to interpret the connect error message. It tells me that connect is concerned with the static information for the class Client, but the slot string indicates the specific instance name - I'm wondering if by the time I get to Model::connectHandle() this name loses its meaning.

It's a simple case of typo:

In class, you have doUpdate() slot.

In main func, you're passing onUpdate() to SLOT() macro.

Also, you shouldn't include the instance in the SLOT() macro, just the slot name (and parameters). Exactly the same syntax you'd use in connect() . Qt's signal-slot connection mechanism is based on string comparison. In other words, your main should do this:

foo.registerForUpdates(SLOT(doUpdate()), cl);
foo.registerForUpdates(SLOT(doUpdate()), cl2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM