How do I attach the private data come along with signal() and send it to the slot()?
-
Hi,
I would like to get the caller's object private data, who call the slot. Any suggestions, please.
The Scenario:
The Package Class Handler -> many classes was called ... -> The Slot Class.The Package Class Handler has to be attached the private data to some object.
void PackgeHandler::unpack(void) { ... // Somehow to attach the private data to "e" and emit the signal to the slot. // How to attach the private data to the object? Please. emit mousePressEvent(e); ... }The Slot Class will take it and see the information.
// I have compiled the container_of() and no error reported. template<class P, class M> size_t cpp_offsetof(const M P::*member) { return (size_t) &( reinterpret_cast<P*>(0)->*member); } template<class P, class M> P* container_of(M* ptr, const M P::*member) { return (P*)( (char*)ptr - cpp_offsetof(member)); } void Button::mousePressEvent(QMouseEvent *e) { // What is A & B? // Assume the attached private data is represented to QObject or other class-type. QObject myObj = container_of(A, B; ... }The PackgeHandler and Button Classes in living on different DLL file, both of them are plug-in.
Can not create new signal / slot, because the architecture has to be no changes.Question:
I do not know, how to use container_of()? Does it work in this way?
Or could you suggest me with any method would work for me?Thank you so much,
Abhisit. -
Hi,
I would like to get the caller's object private data, who call the slot. Any suggestions, please.
The Scenario:
The Package Class Handler -> many classes was called ... -> The Slot Class.The Package Class Handler has to be attached the private data to some object.
void PackgeHandler::unpack(void) { ... // Somehow to attach the private data to "e" and emit the signal to the slot. // How to attach the private data to the object? Please. emit mousePressEvent(e); ... }The Slot Class will take it and see the information.
// I have compiled the container_of() and no error reported. template<class P, class M> size_t cpp_offsetof(const M P::*member) { return (size_t) &( reinterpret_cast<P*>(0)->*member); } template<class P, class M> P* container_of(M* ptr, const M P::*member) { return (P*)( (char*)ptr - cpp_offsetof(member)); } void Button::mousePressEvent(QMouseEvent *e) { // What is A & B? // Assume the attached private data is represented to QObject or other class-type. QObject myObj = container_of(A, B; ... }The PackgeHandler and Button Classes in living on different DLL file, both of them are plug-in.
Can not create new signal / slot, because the architecture has to be no changes.Question:
I do not know, how to use container_of()? Does it work in this way?
Or could you suggest me with any method would work for me?Thank you so much,
Abhisit. -
@aha_1980 Thanks, I can not create the new signal / slot. Because the architecture has to be no changes.
@abhisit said in How do I attach the private data come along with signal() and send it to the slot()?:
Because the architecture has to be no changes
?
How do you want to pass private data without changing the interface?! -
This is a hack to fit the question requirements. It's not the best solution
Assuming that in
emit mousePressEvent(e);,eis of typeQMouseEvent *you could subclassclass DataMouseEvent : public QMouseEvent { public: using QMouseEvent::QMouseEvent; QVariant member; };and change
eto be typeDataMouseEvent. Then inButton::mousePressEvent(QMouseEvent *e)you can justdynamic_cast<DataMouseEvent*>(e)->member;