Results 1 to 3 of 3

Thread: TableView keypress event

  1. #1
    Join Date
    Oct 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default TableView keypress event

    Hi, everyone!
    I wanna ask the solution for the situation:

    My project has MainWindow Class and tableView and textEdit
    (last two were created through the Designer in mainwindow.ui).

    I need to do some action when shortcut pressed in tableView.
    I've got right reaction through the
    Qt Code:
    1. bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    To copy to clipboard, switch view to plain text mode 
    here is the code:
    Qt Code:
    1. ...
    2. // in MainWindow constructor
    3. ui->tableView->installEventFilter(this);
    4. ...
    5.  
    6. bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    7. {
    8. if(watched == ui->tableView && event->type() == QEvent::KeyPress){
    9. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    10. qDebug()<<keyEvent->key();// DO SOMETHING
    11. }
    12. return QWidget::eventFilter(watched, event);
    13. }
    To copy to clipboard, switch view to plain text mode 

    But maybe there is a simpler solution? How to catch keypress in tableView?

    Cause i've created tableView in *.ui i can't simple use keyPressEvent for tableView only for MainWindow (it doesn't recognize tableView or textEdit).

    Thank's in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: TableView keypress event

    Quote Originally Posted by yur0k View Post
    Hi, everyone!
    Cause i've created tableView in *.ui i can't simple use keyPressEvent for tableView only for MainWindow (it doesn't recognize tableView or textEdit).
    Create your own subclass of QTableView with whatever custom behaviour you want. Let's call it MyTableView, defined in mytableview.h and implemented in mytableview.cpp.
    In the main window UI right-click the QTableView and select "Promote to..."
    Provide the promoted class name "MyTableView", and the header file "mytableview.h".
    Click Add.

    The ui compiler should use your custom class name and header file in the generated code.
    Last edited by ChrisW67; 5th October 2020 at 02:07. Reason: spelling corrections

  3. #3
    Join Date
    Oct 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: TableView keypress event

    Ok, i'll try it. Thank you.

Similar Threads

  1. probelm in keypress event
    By iswaryasenthilkumar in forum Newbie
    Replies: 2
    Last Post: 7th April 2015, 08:51
  2. QDialog keypress event
    By bear35645 in forum Qt Programming
    Replies: 6
    Last Post: 6th February 2015, 07:51
  3. problem with keypress event and QTableView
    By ranna in forum Qt Programming
    Replies: 4
    Last Post: 24th August 2009, 17:13
  4. how to combine keypress event and mousebuttonpress event?
    By hildebrand in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2009, 00:08
  5. keypress event
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2007, 15:12

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.