复制 QTableView 的一部分 [英] Copying part of QTableView

查看:49
本文介绍了复制 QTableView 的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个与我在这里看到的另一个问题密切相关的问题,但是当我尝试在那里提出我的问题时,我没有得到任何回应,我希望通过提出这个新问题,有人可以帮助我.基本上,我只想复制我创建的表格的一部分,以便可以将其粘贴到 Excel 文件中.这是我所拥有的:

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('
');
    }
    else
    {
        copy_table.append('	');
    }
    previous = index;
}

QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(copy_table);

这将很好地复制列,但是当我尝试复制行或说 2x2 子表时,行索引会变得混乱,错误地为值分配行索引.有什么想法吗?

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?

推荐答案

嗯,已经想通了,抱歉浪费时间看的人.

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('
');
    }
    else
    {
        copy_table.append('	');
    }
    previous = index;
}

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

QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(copy_table);

}

这篇关于复制 QTableView 的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆