Key press event in a templated class
I just discovered that Q_OBJECTs and templates don’t mix. I have a small templated
class that needs to listen for a QLineEdit’s returnPressed signal. What is the best way
to do this? With installEventFilter? What is the event code? Thanks!
4 replies
The difficulty for me is, this helper class would need a reference to an instance of a template class. Can I do that without the helper class being templated? The only kludgey way I can
see to do this is to use a helper class and an interface. I’m new at C++ templates so
maybe I’m missing something.
Here’s some pseudo-code.
This is what I wish I could do, but can’t because of the q_object/template thing:
- template <class T>
- class Foo {
- constructor {
- QTableView table...
- connect(table, doubleClicked, doubleClicked)
- }
- slot doubleClicked {
- handler.handle(t)
- }
- Handler<T> handler; // member
- }
This is my hack. How can I do this better?
- class Helper {
- Q_OBJECT
- Interface callback; // member
- }
- template <class T>
- class Foo : public Interface {
- constructor {
- QTableView table...
- connect(table, doubleClicked, helper.doubleClicked)
- }
- callbackIndex {
- handler.handle(t)
- }
- Helper helper (this); // member
- Handler<T> handler; // member
- }
The handler is being passed into Foo and Foo has a couple other templated members as well.
I just don’t know how to pass in a reference to Foo in Helper without Helper being templated as well.
If I try to pass it in as a pointer with no template parameter it is an error: Helper::Helper(Foo* foo)
You must log in to post a reply. Not a member yet? Register here!


