Handle keypress with object instead of QListView



  • 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



  • 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);
    }
    @



  • 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



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



  • 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



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



  • 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?



  • 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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.