Results 1 to 7 of 7

Thread: KeyPressEvent in QTableWidgetItem.

  1. #1
    Join Date
    Dec 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default KeyPressEvent in QTableWidgetItem.

    Hi

    I want to capture KeyPressEvent when i press enter after editing any particular cell in QTableWidget.
    I am getting KeyPressEvent for QTableWidget class which is a sub class of QWidget.

    I tried to capture the event for QTableWidgetItem, But i Think its not a subclass of QWidget.
    So how do i capture the KeyPressEvent for QTableWidgetItem?

    Thanks

  2. #2
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyPressEvent in QTableWidgetItem.

    Yes that's true. You might want to use QTableWidget::setCellWidget(int row, int column, QWidget * widget).

    You can set your custom widget for the cell and handle key press events in them.

    It probably depends on what you want to do, also consider:
    • Using delegates
    • setting event filter on QTableWidget's viewport
    Last edited by Vikram.Saralaya; 9th December 2015 at 13:12.

  3. The following user says thank you to Vikram.Saralaya for this useful post:

    akashh (10th December 2015)

  4. #3
    Join Date
    Dec 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: KeyPressEvent in QTableWidgetItem.

    for EventFilter, I cannot again install eventfilter on QTableItemWidget. It seems it does not even inherit QObject class as well so cannot call installEventFilter function.
    Can you please guide about what should be the source and target in case of eventfilter, if i want to receive return key event while editing on a QTableWidgetItem?

    Thanks

  5. #4
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyPressEvent in QTableWidgetItem.

    Event filter has to be installed on QTableWidget's viewport. In any case I can't suggest you a good solution without knowing what you wan't to achieve.

    I can think of a few solutions. How about this one:
    • Connect the signal
      Qt Code:
      1. void QTableWidget::cellPressed(int row, int column)
      To copy to clipboard, switch view to plain text mode 
      to a slot.
    • In the slot you know that QTableWidgetItem corresponding to (row, column) is being edited.
    • Save it.
    • In the custom QTableWidget's keypress event handler, when the return key is pressed you know what to do for the item that is being edited!

  6. #5
    Join Date
    Dec 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: KeyPressEvent in QTableWidgetItem.

    Hi Want to achieve MS Excel like functionality in QTableWidget. Where on pressing Alt+ enter we get the cursor on next line in same cell instead of going to next cell.

  7. #6
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyPressEvent in QTableWidgetItem.

    Then indeed the solution mentioned in my previous post could be a good option.. on keypress event since you already saved the row, column of the item being edited, you can get the item corresponding to (row+1, column) and call QTableWidgetItem::setSelected(true).

  8. The following user says thank you to Vikram.Saralaya for this useful post:

    akashh (10th December 2015)

  9. #7
    Join Date
    Dec 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: KeyPressEvent in QTableWidgetItem.

    I dont want to go to next cell in same column, I want to stay in same cell. What you are suggesting is going to next cell in the same column.
    The solution will work in that case, but as i want to go to next line in the same cell.
    Yeah, I have the values saved in the cell befor key event, and i can append the newline character as well in the string. But after the call of keyPressEvent is completed, the cell is selected, but not in editing mode, any clue why editItem() is not working?

    this->setCurrentCell(this->currentRow() + 1, this->currentColumn());
    this->setCurrentCell(this->currentRow() - 1, this->currentColumn());
    this->currentItem()->setText(this->currentItem()->text() + "\n");
    //this->setCurrentCell(this->currentRow(), this->currentColumn());
    this->editCurrentCell(); // Wrapper for editCell of QTableWidget


    Added after 7 minutes:


    Ok, Got It. editItem will not work unless not acted upon on some other cell.

    this->setCurrentCell(this->currentRow() + 1, this->currentColumn());
    this->editCurrentCell(); //
    this->setCurrentCell(this->currentRow() - 1, this->currentColumn());
    this->currentItem()->setText(this->currentItem()->text() + "\n");
    //this->setCurrentCell(this->currentRow(), this->currentColumn());
    this->editCurrentCell();
    Last edited by akashh; 10th December 2015 at 11:20.

Similar Threads

  1. Some troubles with keyPressEvent
    By code_err in forum Newbie
    Replies: 2
    Last Post: 3rd March 2012, 03:28
  2. keypressevent
    By djwk in forum Newbie
    Replies: 9
    Last Post: 5th July 2010, 03:12
  3. How to block keyPressEvent ?
    By richardander in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2009, 10:14
  4. Replace keyPressEvent (dot with tab)
    By the_bis in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2008, 11:43
  5. QTreeWidget keyPressEvent
    By hgedek in forum Qt Programming
    Replies: 3
    Last Post: 18th August 2007, 17:33

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