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
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
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
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 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!
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.
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).
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();