PDA

View Full Version : Detecting when the delete key was pressed on a listwidget item



scwizard
27th February 2007, 02:19
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.

jpn
27th February 2007, 08:14
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.

scwizard
27th February 2007, 13:42
That should work.
Qt has a lot of classes. I'm kind of thinking I should print out the Annotated Class Index (http://doc.trolltech.com/4.2/annotated.html) and read the whole thing so I'll have more of an idea where to look when I want to do something.

bpetty
28th February 2007, 21:26
QShortcut* shortcutDelQueue = new QShortcut(QKeySequence(Qt::Key_Delete), ui.lstwQueue);

scwizard
1st March 2007, 01:57
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?

jpn
1st March 2007, 08:19
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.. :)