Results 1 to 7 of 7

Thread: QTableView key pressed

  1. #1
    Join Date
    Oct 2007
    Location
    Romania
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableView key pressed

    In my project I use a QTableView and I want to delete the current row when the user press del. I want to catch the signal keyPressEvent(QKeyEvent*) of the tableview but it does not work do you know why? I used :

    connect(ui.categoriesTree,SIGNAL(keyPressEvent(QKe yEvent*)),this,SLOT(FilesKeyPressEvent(QKeyEvent*) ));

    and I don’t get the function executed. Thanks a lot.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView key pressed

    That's an event, not a signal. You have to subclass QTableView and override it.

  3. #3
    Join Date
    Oct 2007
    Location
    Romania
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView key pressed

    Is there any other way to delete a row when the user press del without sub classing QTableView ?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView key pressed

    You could use QShortcut:
    Qt Code:
    1. QShortcut* shortcut = new QShortcut(QKeySequence(QKeySequence::Delete), tableView);
    2. connect(shortcut, SIGNAL(activated()), receiver, SLOT(deleteRow()));
    3.  
    4. void Receiver::deleteRow()
    5. {
    6. QModelIndex idx = tableView->currentIndex();
    7. if (idx.isValid())
    8. tableView->model()->removeRow(idx.row(), idx.parent());
    9. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following 2 users say thank you to jpn for this useful post:

    Ahmed Abdellatif (28th August 2018), AlphaWolfXV (1st August 2013)

  6. #5
    Join Date
    Oct 2007
    Location
    Romania
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QTableView key pressed

    Thanks. I think that this will work.

  7. #6
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView key pressed

    Hi JP-N,
    I am trying a similar thing with the Escape key in a QTableView. I am using the QShortcut, tied to the qtableview and then connecting the activated signal.

    What happens is that it works fine as long as I am not editing a row in the qtableview, if data entry is occurring in the table, the first "Esc" key sequence resets the table to the pre-edit state, and the second and subsequent presses catch fine.

    My question is how can I intercept the first ESC key sequence, utilizing the QShortcut?
    Qt Code:
    1. keyEsc = new QShortcut(QKeySequence(Qt::Key_Escape),ui->tableView);
    2. connect(keyEsc,SIGNAL(activated()),this,SLOT(ESCPressed_tblview()));
    To copy to clipboard, switch view to plain text mode 
    Thanks,
    AlphaWolfXV

  8. #7
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView key pressed

    For anyone interested, this did work but required first re-implementing the eventFilter in a lineEditDelegate class based on qItemDelegate. Please see this for additional information on this solution!
    Thanks,
    AlphaWolfXV

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.