PDA

View Full Version : Copy text from QTableView



Edder
16th August 2012, 10:24
Hi,

I try to copy selected data from a qtableview to clipboard, seperated with '\t' for columns and '\n' for rows.
Its working just fine, but only when I select numbers, if I select text its screwed up and only the last item shows on pasting.

I've overridden the keyPressEvent:


void keyPressEvent(QKeyEvent *event)
{
if (event->matches(QKeySequence::Copy))
{
QAbstractItemModel *abmodel = this->model();
QItemSelectionModel *model = this->selectionModel();
QModelIndexList list = model->selectedIndexes();

qSort(list);

if(list.size() < 1)
return;

QString copy_table;
QModelIndex last = list.last();
QModelIndex previous = list.first();

list.removeFirst();

for(int i = 0; i < list.size(); i++)
{
QVariant data = abmodel->data(previous);
QString text = data.toString();
QModelIndex index = list.at(i);
copy_table.append(text);

if(index.row() != previous.row())
copy_table.append('\n');
else
copy_table.append('\t');
previous = index;
}

copy_table.append(abmodel->data(list.last()).toString());
copy_table.append('\n');

QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(copy_table);
}
QTableView::keyPressEvent(event);
};
And the QString copy_table holds the correct text, but it isnt set correctly to the clipboard.

To clarify
Works:


230 26 26
303 26 26
528 17 17

Doesnt Work:


4602 4601 Orgrimmar Grunt
374 4639 Southsea Brigand
4638 4640 Southsea Cutthroat

In this case, only "Southsea Cutthroat" will be the content of the clipboard.
And I really dont know why, somebody knows a solution for this?

Edder
18th August 2012, 08:15
Got it!

Something is happening with the clipboard in the QTableView::keyPressEvent if its a Copy Keysequence.

Adding a:


else
QTableView::keyPressEvent(event);

at the end, so the copy sequence doesnt get forwarded to the qtableview keypressevent, resolved the problem.

Still I'm curios, what happens with text at the keypressevent that doesnt happen with numbers?