View Full Version : Copy/Paste in TableView

10th September 2006, 09:49
Hi all,

I am using a model/view/delegate and I subclass QTableView to reimplement keyPressEvent(QKeyEvent *event) to catch ctrl-c and ctrl-v. I use the clipboard to copy the text at the currentIndex() and paste the results with model->setData(..). The method looks like this:

void TableViewEx::keyPressEvent( QKeyEvent *event )
if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {

QApplication::clipboard()->setText( this->currentIndex().data().toString() );

else if (event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_P) {

model()->setData( currentIndex(), QApplication::clipboard()->text() );

else {



The problem I'm having is that pasting text into a table-cell does not occur unless I use the Shift key also [CTRL-Shift-P].

Any ideas what might be causing this?

Added code tags/e8johan

10th September 2006, 13:19
Maybe the CTRL+P combination is already assigned somewhere? Have you tried using CTRL+V?

10th September 2006, 22:36

After changing keyPressEvent(..) to catch CTRL+V,

else if (event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_V)

copy/paste works as expected.

As it turns out, the original post used Qt::Key_P which is a post-midnight coding bug. Using that catch, paste works using CTRL+P.