Results 1 to 5 of 5

Thread: QTableView and Key-Delete

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView and Key-Delete

    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 

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView and Key-Delete

    Have you tried subclassing QTableView or installing an event filter? With the former, your function will only be called when the table has focus.

  3. #3
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView and Key-Delete

    adding an event-filter like shown below gives the same result, the delete key is not shown. When I subclass QTableView, what do I have to overwrite to get all keys?

    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. bool eventFilter(QObject* object, QEvent* event);
    12. };
    13. MyApp::MyApp() : QWidget()
    14. {
    15.  
    16. table = new(QTableView);
    17. model = new QStandardItemModel(2, 1);
    18. table->setModel( model );
    19.  
    20. for( int r=0; r<2; r++ )
    21. {
    22. item = new QStandardItem(QString("Test"));
    23. model->setItem(r, 0, item);
    24. }
    25.  
    26. table->setSelectionMode( QAbstractItemView::SingleSelection );
    27. table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    28.  
    29. layout = new QVBoxLayout;
    30. layout->addWidget(table);
    31. setLayout(layout);
    32. installEventFilter(this);
    33. }
    34. MyApp::~MyApp()
    35. {
    36. }
    37. void MyApp::keyPressEvent(QKeyEvent *event)
    38. {
    39. switch (event->key())
    40. {
    41. case Qt::Key_Return:
    42. case Qt::Key_Enter:
    43. qDebug() << "Enter";
    44. break;
    45. case Qt::Key_Escape:
    46. qDebug() << "Escape";
    47. break;
    48. case Qt::Key_Insert:
    49. qDebug() << "Insert";
    50. break;
    51. case Qt::Key_Delete:
    52. qDebug() << "Delete";
    53. break;
    54. default:
    55. qDebug() << "other" << event->key();
    56. break;
    57. }
    58. }
    59. bool MyApp::eventFilter(QObject* object, QEvent* event)
    60. {
    61. if (event->type()==QEvent::KeyPress)
    62. {
    63. QKeyEvent* pKeyEvent=static_cast<QKeyEvent*>(event);
    64. if (pKeyEvent->key() == Qt::Key_Delete)
    65. {
    66. if (table->hasFocus())
    67. {
    68. qDebug() << "Event filter: Focus yes, Delete key pressed";
    69. }
    70. else
    71. {
    72. qDebug() << "Event filter: Focus NO, Delete key pressed";
    73. }
    74. return TRUE;
    75. }
    76. qDebug() << "Event filter: other key pressed";
    77. }
    78. return QWidget::eventFilter(object, event);
    79. }
    80.  
    81. int main( int argc, char **argv )
    82. {
    83. QApplication app( argc, argv );
    84. MyApp Test;
    85.  
    86. Test.show();
    87.  
    88. return app.exec();
    89. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView and Key-Delete

    You are installing an event filter MyApp (itself). Try table->installEventFilter(this) which would install it where you want it.

    Alternatively, try to override QAbstractItemView::keyPressEvent

  5. The following user says thank you to squidge for this useful post:

    Armin (9th January 2010)

  6. #5
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView and Key-Delete

    Now it works, many thanks for yout help.
    regards Armin

Similar Threads

  1. Replies: 2
    Last Post: 26th November 2009, 04:45
  2. Replies: 4
    Last Post: 19th February 2009, 11:10
  3. new, but no delete
    By Windsoarer in forum Qt Programming
    Replies: 2
    Last Post: 10th December 2008, 22:20
  4. delete
    By mickey in forum General Programming
    Replies: 1
    Last Post: 13th July 2006, 12:13

Tags for this Thread

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.