Disable the default behavior of the QlistWidget of chaging focus when keyboard key press matches first letter of text in a item



  • Hi Everyone,

    So I'm just new at QT and I have Qlstwidget and im using de 'D' key to disable(changes icon) of items from the list, but every time I press D and there are items that start with the letter D the focus/selection/current item changes and it's just anoying , is there a way to disable this default behavior, so when I press a letter doesn't go to the items starting with that letter?

    thanks in Advance,



  • Hi,

    If you inherit from QListWidget, you can override the keyPressEvent() function, and handle the press on D however you want.
    Something like:

    // Class declaration. Inherit from QlstWidget
    class MyWidget : public QlstWidget {
    protected: 
    void keyPressEvent(QKeyEvent *event) ;
    }
    
    // And implement the keyPressEvent method
    MyWidget::keyPressEvent(QKeyEvent *event) {
           if(event->key() == Qt::key_D) {
                     //disable item from the list
           }
           else {
                    // call the native function in any other case (or don't, if you never care about the QlstWidget receiving other key events)
                    Qlstwidget::keyPressEvent(event);
           }
    

    If you don't want to derive your own class from Qlstwidget, you can also use an eventfilter (http://doc.qt.io/qt-5/qobject.html#installEventFilter).

    Eventfilters allow you to "intercept" the event destined to another widget. You can then decide to call some methods depending on this event, and then relay the item to its original destination (if desired).


Log in to reply
 

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