Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Key press event in a templated class
QtWS25 Last Chance

Key press event in a templated class

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    reactive
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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(...))

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        reactive
        wrote on last edited by
        #3

        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 {
        T t = ... calculated from QModelIndex
        handler.handle(t)
        }

        Handler<T> handler; // member

        }
        @

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

        @
        class Interface { public: callBackIndex(QModelIndex) }

        class Helper {

        Q_OBJECT

        slot doubleClicked { callback.callbackIndex(QModelIndex); }

        Interface callback; // member

        }

        template <class T>
        class Foo : public Interface {

        constructor {
        QTableView table...
        connect(table, doubleClicked, helper.doubleClicked)
        }

        callbackIndex {
        T t = ... calculated from QModelIndex
        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)

        1 Reply Last reply
        0
        • R Offline
          R Offline
          reactive
          wrote on last edited by
          #4

          Bump.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Franzk
            wrote on last edited by
            #5

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

            "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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved