Copying part of QTableView

2020-03-14 20:11发布

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?

3条回答
太酷不给撩
2楼-- · 2020-03-14 20:41

I wrote some code based on Phil's to copy the selection when the user types Control-C.

I subclassed QTableWidget and overrode keyPressEvent():

void MyTableWidget::keyPressEvent(QKeyEvent* event) {
    // If Ctrl-C typed
    // Or use event->matches(QKeySequence::Copy)
    if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier))
    {
        QModelIndexList cells = selectedIndexes();
        qSort(cells); // Necessary, otherwise they are in column order

        QString text;
        int currentRow = 0; // To determine when to insert newlines
        foreach (const QModelIndex& cell, cells) {
            if (text.length() == 0) {
                // First item
            } else if (cell.row() != currentRow) {
                // New row
                text += '\n';
            } else {
                // Next cell
                text += '\t';
            }
            currentRow = cell.row();
            text += cell.data().toString();
        }

        QApplication::clipboard()->setText(text);
    }
}

Output example (tab-separated):

foo bar baz qux
bar baz qux foo
baz qux foo bar
qux foo bar baz
查看更多
3楼-- · 2020-03-14 20:52

Well, already figured it out, sorry anyone that wasted their time and looked.

void TestCopyTable::on_pushButton_copy_clicked()
{
QAbstractItemModel *abmodel = ui.tableView->model();
QItemSelectionModel * model = ui.tableView->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);

}

查看更多
Evening l夕情丶
4楼-- · 2020-03-14 20:54

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.

查看更多
登录 后发表回答