Hello to all,

I'm new to Qt (using it for 4 weeks, used c++ builder before), using Qt 4.6 with C++ on windows (Qt Creator) and have a small problem detecting press of Key_Delete in a QTableView to manage to delete the whole row of data. I used keyPressEvent(QKeyEvent*) which is triggered on other Keys, but not the delete key. Below is a small example of compilable code, maybe someone has a hint for me. If I comment out the setEditTrigger line, I get the event once (and the contents of the active item is deleted), but then the tableview has no focus when I query that in keyPressEvent() (I later only want to act on delete key when the table has focus).

Thanks in advance
Armin

Qt Code:
  1. #include <QtGui>
  2.  
  3. class MyApp : public QWidget
  4. {
  5. public:
  6. MyApp();
  7. ~MyApp();
  8. QTableView *table;
  9. QVBoxLayout *layout;
  10. void keyPressEvent(QKeyEvent *event);
  11. };
  12. MyApp::MyApp() : QWidget()
  13. {
  14.  
  15. table = new(QTableView);
  16. model = new QStandardItemModel(2, 1);
  17. table->setModel( model );
  18.  
  19. for( int r=0; r<2; r++ )
  20. {
  21. item = new QStandardItem(QString("Test"));
  22. model->setItem(r, 0, item);
  23. }
  24.  
  25. table->setSelectionMode( QAbstractItemView::SingleSelection );
  26. table->setEditTriggers(QAbstractItemView::NoEditTriggers);
  27.  
  28. layout = new QVBoxLayout;
  29. layout->addWidget(table);
  30. setLayout(layout);
  31. }
  32. MyApp::~MyApp()
  33. {
  34. }
  35. void MyApp::keyPressEvent(QKeyEvent *event)
  36. {
  37. switch (event->key())
  38. {
  39. case Qt::Key_Return:
  40. case Qt::Key_Enter:
  41. qDebug() << "Enter";
  42. break;
  43. case Qt::Key_Escape:
  44. qDebug() << "Escape";
  45. break;
  46. case Qt::Key_Insert:
  47. qDebug() << "Insert";
  48. break;
  49. case Qt::Key_Delete:
  50. qDebug() << "Delete";
  51. break;
  52. default:
  53. qDebug() << "other" << event->key();
  54. break;
  55. }
  56. }
  57. int main( int argc, char **argv ) {
  58. QApplication app( argc, argv );
  59. MyApp Test;
  60.  
  61. Test.show();
  62.  
  63. return app.exec();
  64. }
To copy to clipboard, switch view to plain text mode