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:
{
{
QModelIndexList list = model->selectedIndexes();
qSort(list);
if(list.size() < 1)
return;
list.removeFirst();
for(int i = 0; i < list.size(); i++)
{
QVariant data
= abmodel
->data
(previous
);
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');
clipboard->setText(copy_table);
}
};
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);
};
To copy to clipboard, switch view to plain text mode
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
230 26 26
303 26 26
528 17 17
To copy to clipboard, switch view to plain text mode
Doesnt Work:
4602 4601 Orgrimmar Grunt
374 4639 Southsea Brigand
4638 4640 Southsea Cutthroat
4602 4601 Orgrimmar Grunt
374 4639 Southsea Brigand
4638 4640 Southsea Cutthroat
To copy to clipboard, switch view to plain text mode
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?
Bookmarks