Results 1 to 6 of 6

Thread: Detecting when the delete key was pressed on a listwidget item

  1. #1
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Detecting when the delete key was pressed on a listwidget item

    I'm not sure what the best practice way is to get a specific listwidget to capture keyboard input. There isn't any kind of itemDoubleClicked( QListWidgetItem* item, QKey? keyID) signal.
    I'm not sure if I should use QAction or not with a hotkey, if I do that I need to make a custom class with extra slots.

    I'm pretty sure I know how I could make a custom class extending QListWidget, and reimplement the keyPressEvent function, but I'm pretty sure there's a simple and elegant way to do this. I'm just not sure what that way is.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Detecting when the delete key was pressed on a listwidget item

    I'd suggest using QShortcut. A custom slot is needed for the removal, though. As far as I remember, QActions only trigger when they're actually placed somewhere like into a menu.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    scwizard (27th February 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting when the delete key was pressed on a listwidget item

    That should work.
    Qt has a lot of classes. I'm kind of thinking I should print out the Annotated Class Index and read the whole thing so I'll have more of an idea where to look when I want to do something.

  5. #4
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Detecting when the delete key was pressed on a listwidget item

    QShortcut* shortcutDelQueue = new QShortcut(QKeySequence(Qt::Key_Delete), ui.lstwQueue);

  6. #5
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting when the delete key was pressed on a listwidget item

    Quote Originally Posted by jpn View Post
    A custom slot is needed for the removal, though.
    Is there any way to add a custom slot to an existing class so I don't need to create a whole new class?

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Detecting when the delete key was pressed on a listwidget item

    Quote Originally Posted by scwizard View Post
    Is there any way to add a custom slot to an existing class so I don't need to create a whole new class?
    Basically any QObject derivated class having access to the list widget goes. So if you already have subclassed for example QMainWindow and the list widget is inside it, you could add the slot into the main window and do the deletion from there. However, don't be afraid of subclassing. I do personally like subclassing a lot and tend to put myself the functionality where it belongs and makes the most sense..
    J-P Nurmi

Similar Threads

  1. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.