2017-01-30 29 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