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. Handle keypress with object instead of QListView
Forum Updated to NodeBB v4.3 + New Features

Handle keypress with object instead of QListView

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 5.4k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    ThaRez
    wrote on last edited by
    #1

    Hello
    I have a object that embeds an QListView inside a layout of the object. My issue is, that it hijacks the keyPressEvents from the user. There is not supposed to be any focus, but instead the UI should work solely with the keyboard. The keyRelease event works just fine. I've inherited the QListView class to implement some custom scrolling functionality. I now wonder how I could pass the keypress event forward to the parent object? If I implement a keyPressEvent function in my custom QListView class that only prints the pressed key

    @void LogList::keyPressEvent(QKeyEvent *e)
    {
    qDebug() << "KeyPress" << e->key();
    }@

    I get the following error while compiling:

    loglist.cpp:48: error: invalid use of incomplete type 'struct QKeyEvent'
    /home/jh/qt-4.7.3/include/QtGui/qwidget.h:79: error: forward declaration of 'struct QKeyEvent'
    make: *** [loglist.o] Error 1

    I'd appreciate any suggestions how to solve this... Thank you
    Richard

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      To fix the compilation issue, simply add this to the top of your source file:
      @
      #include <QKeyEvent>
      @

      If you reimplement the key event, you most probably will want to forward the event to the inherited class implementation like this:
      @
      void LogList::keyPressEvent(QKeyEvent *e)
      {
      qDebug() << "KeyPress" << e->key();
      QListView::keyPressEvent(e);
      }
      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ThaRez
        wrote on last edited by
        #3

        Hello
        And thank you. That's precisely what I'd like to do, though I'm still getting the same forward error... Any ideas why?
        Richard

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          You are not getting an include error earlier on somewhere in your compilation output?

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ThaRez
            wrote on last edited by
            #5

            Hello
            You were right about the missing inclusion. It's now working to the extent that I'm able to catch the key in the child class, and but the forwarding does not yet work. One thing I forgot to mention is that the child class isn't directly embedded by the parent that is supposed to capture the key, and but one level below that (mainclass-> layout->qlistview) though the layout class has no keypressevent impented. Do I need to implement it and pass it via it as well? Thank you.
            Richard

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris H
              wrote on last edited by
              #6

              Andre, is this an example of a situation where using <code>eventFilter()</code> might make more sense?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                It could, but I am not exactly sure what the goal of ThaRez is exactly. I first thought that he was trying to subclass QListView, but perhaps not?

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  ThaRez
                  wrote on last edited by
                  #8

                  Hello
                  To clarify the situation a bit further, I have a class that is responsible for creating a layout (layout. cpp) and does this by combining different components (class title, keywords main and footer.cpp). These take different forms depending on the parameters passed. The main.cpp tht again embeds the custom qlistview (logistics. cpp). This is the basic setup. I what I want to accomplish is for layout.cpp to handle all the keypressevent. My current problem is that the logistics catches these. I do not know about how event filters work (have to check it) but it's possible that they might work as well. So far I've managed to catch the key keypressevent in the logistics. cpp class, and but unable to pass it down to the layout. cpp. I'd like to thank you for your help so far.
                  Best regards
                  Richard

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #9

                    An event filter is surely fine for your purpose. Just remember to install it on the view's viewport()...

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    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