2010-06-28 6 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); 

이 열 벌금을 복사합니다,하지만 난 행을 복사하거나 말을 할 때 2 × 2 잘못 값의 행 인덱스를 지정, 행 인덱스가 엉망이됩니다 서브 테이블 : 여기있는거야. 이견있는 사람?

답변

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

나는 선택 사용자 유형을 제어-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

벌금, 사용할 준비가 코드. +1 특히 세포의 qSort. 저를 잠시 넘어 뜨 렸을 것입니다 – Mizmor

+1

http://stackoverflow.com/questions/1230222/selected-rows-line-in-qtableview-copy-to-qclipboard : event-> matches (QKeySequence :: 복사) ctrl + c를 수동으로 확인하는 대신 – Legolas

+0

소리가 더 좋아. –