1
votes

I want to wrap the Qt connect method (Qt5 syntax), but can't get my code to compile. The simplest example I can come up with is (not tested, just general idea):

// Called like this 
doConnect(&obj1,&obj1:sig,&obj2,&obj2::slot);

// Implemented here
void namespace::doConnect(
  const QObject* senderObject, 
  const QMetaMethod& senderSignal, {
  const QObject* receiverObject, 
  const QMetaMethod& receiverSlot) {
    connect(senderObject, senderSignal, receiverObject, receiverSlot);
}

When I try to compile this I get an error on the line that calls doConnect,

"no matching function for call to doConnect"

and on the connect line

error: static assertion failed: Signal and slot arguments are not compatible.

Which sounds like signal and slot have different/incompatible arguments. However, if I call connect directly by putting the sender object/signal and receiver object/slot in the connect command it works fine. So it's not signature of signal/slot issue.

What is wrong here? Does passing the parameters to a function first hide some information that the connect function (or macro or template) needs?

1
It would be nice to see the actual call and the exact signature of the functiins passed as signal & slot.Benjam

1 Answers

0
votes

For me the following works well:

void doConnect(const QObject* senderObject, const QMetaMethod& senderSignal, const QObject* receiverObject, const QMetaMethod& receiverSlot)
{
    QObject::connect(senderObject, senderSignal, receiverObject, receiverSlot);
}

You've write "Signal and slot arguments are not compatible" from your compiler. And that's not strange. You could try to do the following: QObject::connect(&obj1,&obj1:sig,&obj2,&obj2::slot);

You'll get the same! Your functions signatures are not compatible. For example: QObject::connect(obj1, SIGNAL("remove(int)"), obj2, SLOT("handleRemove(QString)"); Just that with this connect, you'll get the error at runtime and not at compile time.

It's the same as: QObject::connect(obj1, &C1::remove, obj2, &C2::handleRemove);

You'll get the same error, because yes, an int is not a QString. Simple and fast. The same applies on the number of arguments, if there's more or less, you can go through that.

Moreover, creating a function doConnect that is the same as QObject::connect is useless. Be more careful when you read the docs (if you did).