So I have a question very closely related to another question I've seen on here but when I tried posing my question there I got no responses, I'm hoping by asking this as a fresh question someone can help me out. Basically I want simply copy a portion of my table that I've created so that I can paste it to an excel file. Here's what I have:
QAbstractItemModel *abmodel = ui.tableview->model();
QItemSelectionModel *model = ui.tableview->selectionModel();
QModelIndexList list = model->selectionIndexes();
qSort(list);
QModelIndex index = list.first();
for(int i = 0; i < list.size(); i++)
{
QModelIndex index = list.at(i);
QString text = abmodel->data(index).toString();
copy_table.append(text);
if(index.row() != previous.row())
{
copy_table.append('\n');
}
else
{
copy_table.append('\t');
}
previous = index;
}
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(copy_table);
This will copy a column fine, but when I attempt to copy a row or say a 2x2 subtable the row index gets messed up, incorrectly assigning the row index for the values. Any thoughts?
I wrote some code based on Phil's to copy the selection when the user types Control-C.
I subclassed
QTableWidget
and overrodekeyPressEvent()
:Output example (tab-separated):
Well, already figured it out, sorry anyone that wasted their time and looked.
}
Regarding the cdline: qSort(cells); // Necessary, otherwise they are in column order currently(20190118) it brings a warning: Warnung: 'qSort >' is deprecated: Use std::sort
so my solution to replace the line with: std::sort(cells.begin(),cells.end()); Compile, Run OK -> so far so good. But Question: Benefit of this cdline? I found there is no one. Made several Test with copy from gui and parsing it to excel. Everything was fine even with the scenario 2x2 or othe XxY.