2010-06-28 98 views
8

所以我有一个非常密切相关的问题,我在这里看到的另一个问题,但当我试图提出我的问题时,我没有回应,我希望通过问这是一个新鲜的问题,有人可以帮帮我。基本上我想简单地复制我创建的表的一部分,这样我就可以将它粘贴到一个excel文件中。下面是我有:复制QTableView的一部分

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); 

这将复制柱很好,但是当我试图复制行或说一个2x2的子表行索引被搞砸,不正确地分配这些值的行索引。有什么想法吗?

回答

13

好吧,已经算出来了,对不起有人浪费了他们的时间和期待。

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); 

}

4

我写了一个基于对菲尔的复制的选择,当用户键入Ctrl-C组合一些代码。

我子类QTableWidget和推翻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); 
    } 
} 

输出实例(制表符分隔):

foo bar baz qux 
bar baz qux foo 
baz qux foo bar 
qux foo bar baz 
+0

甲细,准备使用的代码段。特别是对于单元格的qSort +1。这会绊倒我一段时间 – Mizmor 2014-11-07 16:04:21

+1

从http://stackoverflow.com/questions/1230222/selected-rows-line-in-qtableview-copy-to-qclipboard:你可以使用event-> matches(QKeySequence ::复制)而不是手动检查ctrl + c – Legolas 2014-11-26 15:23:42

+0

啊,听起来更好 – 2014-11-26 22:41:34