C++ function QObject::connect with paramter QObject and function pointer ;
-
i want to write ConnectMathod(...) in such way that it accept QObject* and receiver slot. and establish connection
//pseudo code as belowclass A : public QObject {
public :
A();
~A();
signals :
void sigA(int);
slots :
void slotA(bool);
}class B : public QObject {
public :
B();
~B();
signals :
void sigB(bool);
slots :
void slotB(int);
}// class C : i want to write common mathod which expect receiver (QObject*) and slot
class C : public QObject
{
public :
C();
~C();
signals :
void signalC(bool);// i want help to write ConnectMathod
QMetaObject::Connection ConnectMathod(QObject* receiverObject, functionPointer)
{
QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
return connection;
}
}/////// main.cpp ///////
main()
{
A objA = new A();
B objB = new B();
C objC = new C();
QMetaObject::Connection connection = objC->ConnectMathod(objA, objA->slotA);
connection = objC->ConnectMathod(objB, objB->slotB);
} -
What's your actual question/error message and please use the code tags to make your code more readable.
-
@JoeCFD its pseudo code.
i need help to write below function corretly, i am doin it wrong
// i want help to write ConnectMathod
QMetaObject::Connection ConnectMathod(QObject* receiverObject, functionPointer)
{
QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
return connection;
} -
@Anas_Deshmukh Then take a look at the definition of QObject::connect() and you will see how to pass it. Hint: you have to make it a template function.
-
Hi,
Take a look at the connect methods implementation.
That said, why do you need that version in the first place ?
-
@Christian-Ehrlicher yes, thats where i am facing trouble. any work around with lambda functions?
-
Perhaps I am missing something here. What does writing this method provide you that is not already provided? This seems to be the equivalent of what you seek and does not require reinventing the wheel:
A *objA = new A(); B *objB = new B(); C *objC = new C(); QMetaObject::Connection connection1 = QObject::connect(objC, &C::signalC, objA, &A::slotA); QMetaObject::Connection connection2 = QObject::connect(objC, &C::signalC, objB, &B::slotB);
-
@ChrisW67
For unknown reason, I think the OP wants a "utility/helper"connect()
method which has its own inbuilt signal-side parameters forconnect()
but accepts the slot-side as parameters. Suppose what he wants is:QMetaObject::Connection C::ConnectMethodForSignalC(QObject* receiverObject, SomeGenericFuncType *functionPointer) { someLogMethod(receiverObject); someLogmethod(functionPointer); // potentially some more code // so that this is all worth factoring into its own function QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer); return connection; }
and he wants to know what this method declaration needs to look like to do it.
As @Christian-Ehrlicher has noted, he will need to declare this as a template function.
[One thing to note is: the use of
this
for the signalling object appears to mean the connection is being made from the signalling class to some slot object. Which is usually not the way to do things: signallers should not know about slotters, usually connections are made from the slot object class, or something which knows about the slot object, not the other way round.@Anas_Deshmukh: Your desire to write this helper method in the signalling class
i want to write common mathod which expect receiver (QObject*) and slot
is "questionable" or "inadvisable", in terms of common practice. I take the point that this is only a "generic" function, and only accepts information about the slot as parameters, but still....]