2017-01-30 31 views
0

剪貼板我已經寫了代碼,我需要選擇行中的Qt C++複製到從tableview中

OTPWindow.cpp文件有 此功能

SafeOTPWindow::on_tblCopy_clicked() 
{ 
    QClipboard* clip = qApp->clipboard(); 
    clip->setText(ui->tblLog->text()); 
} 

OTPWindow.h文件有

private slots: 
    void on_tblCopy_clicked(); 
複製

我得到一個錯誤

文本不是Qtableview的成員。我該如何解決這個錯誤

我需要從tableview複製文本內容,這些文本內容應該在.cpp文件中設置什麼屬性。這裏tblLog是我的tableview

+0

是我的回答有幫助嗎? –

+0

是的,這工作。我也嘗試過使用另一種在Doubleclick上使用的方法,它也可以工作 – cyley

回答

0

的方式來實現你想要的是獲得所選項目的列表,並串聯這些項目的文本,就像如下:

QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model()); 

if (!model) //Check if listview has a model 
    return; 

QModelIndexList indexlist = ui->tableView->selectionModel()->selectedIndexes(); 

QString str; 

int lastrow = -1; 

foreach (const QModelIndex &index, indexlist) 
{ 
    if (lastrow >= 0) 
    { 
     if (index.row() == lastrow) 
      str.append(Qt::Key_Space); //Add space between items in same line 
     else 
      str.append("\n"); //Add break line if entering in a new line 
    } 

    str.append(model->index(index.row(), index.column()).data().toString()); 

    lastrow = index.row(); 
} 

str.append("\n"); //Add break line to the end of the string 

QClipboard* clipboard = QApplication::clipboard(); 
clipboard->setText(str); //Copy the string to clipboard