Let's assume you have a list widget in a main window. Then, a natural place to create the shortcut would be in the constructor of the main window (or a function called from there). In addition, you'll need a slot where to react to the shortcut event.
class MainWindow...
{
...
private slots:
void deleteItem();
private:
};
class MainWindow...
{
...
private slots:
void deleteItem();
private:
QListWidget* listWidget;
};
To copy to clipboard, switch view to plain text mode
MainWindow::MainWindow(...) : ...
{
// create the shortcut after the list widget has been created
// option A (pressing DEL anywhere in the main window activates the slot)
// option B (pressing DEL activates the slots only when list widget has focus)
connect(shortcut, SIGNAL(activated()), this, SLOT(deleteItem()));
}
void MainWindow::deleteItem()
{
delete listWidget->currentItem();
}
MainWindow::MainWindow(...) : ...
{
// create the shortcut after the list widget has been created
// option A (pressing DEL anywhere in the main window activates the slot)
new QShortcut(QKeySequence(Qt::Key_Delete), this, SLOT(deleteItem()));
// option B (pressing DEL activates the slots only when list widget has focus)
QShortcut* shortcut = new QShortcut(QKeySequence(Qt::Key_Delete), listWidget);
connect(shortcut, SIGNAL(activated()), this, SLOT(deleteItem()));
}
void MainWindow::deleteItem()
{
delete listWidget->currentItem();
}
To copy to clipboard, switch view to plain text mode
Bookmarks