2010-06-28 61 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