April 26, 2011

reactive reactive
Lab Rat
59 posts

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

April 26, 2011

Volker Volker
Robot Herder
5428 posts

You can create a private helper class which inherits QObject, instantiate one object in your template class and use this as the receiver for the signal (connect is a static method – your template class can just call QObject::connect(…))

May 13, 2011

reactive reactive
Lab Rat
59 posts

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:

  1. template <class T>
  2. class Foo {
  3.  
  4. constructor {
  5.    QTableView table...
  6.    connect(table, doubleClicked, doubleClicked)
  7. }
  8.  
  9. slot doubleClicked {
  10.    T t = ... calculated from QModelIndex
  11.    handler.handle(t)
  12. }
  13.  
  14.    Handler<T> handler; // member
  15.  
  16. }

This is my hack. How can I do this better?

  1. class Interface { public: callBackIndex(QModelIndex) }
  2.  
  3. class Helper {
  4.    
  5.    Q_OBJECT
  6.    
  7.    slot doubleClicked { callback.callbackIndex(QModelIndex); }
  8.  
  9.    Interface callback; // member
  10.  
  11. }
  12.  
  13. template <class T>
  14. class Foo : public Interface {
  15.  
  16. constructor {
  17.    QTableView table...
  18.    connect(table, doubleClicked, helper.doubleClicked)
  19. }
  20.  
  21. callbackIndex {
  22.    T t = ... calculated from QModelIndex
  23.    handler.handle(t)
  24. }
  25.  
  26.    Helper helper (this); // member
  27.    Handler<T> handler; // member
  28.  
  29. }

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)

May 16, 2011

reactive reactive
Lab Rat
59 posts

Bump.

May 16, 2011

Franzk Franzk
Lab Rat
830 posts

Depending on what T you intend your template for, you can consider using QVariant instead.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

 
  ‹‹ Qt libraries 4.7.2 for Windows (Visual Studio 2008)      [Solved] basic functions with QTreeView populated with subclassed QFileSystemModel ››

You must log in to post a reply. Not a member yet? Register here!