从tableview

时间:2017-01-30 13:33:00

标签: c++ qt uitableview

我已编写代码,我需要在Qt C ++中复制选定的行

我的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文件中我应该设置什么属性的行中。此处tblLogtableview

1 个答案:

答案 0 :(得分: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
相关问题