O
O
Oleg Seledets2019-01-05 21:28:14
Qt
Oleg Seledets, 2019-01-05 21:28:14

How to copy selected data from one TableView to another TableView?

Hello there are two tables qTableView.
When you double-click on a cell in the first one, you need to move this cell to the second table.
did it this way:

void reportHelp::on_tableView_doubleClicked(const QModelIndex &index)
{
    QStandardItemModel *model = new QStandardItemModel;
    QStandardItem *item;
    QStringList horizontalHeader;

    horizontalHeader.append("Судовладельцы");
    model->setHorizontalHeaderLabels(horizontalHeader);

    //проверка
    ui->textEdit->setText(index.data().toString());
    ui->lineEdit->setText(QString::number(row));

    item = new QStandardItem(QString(index.data().toString()));
    model->insertRow(row,item);
    row++;

    ui->tableView_2->setModel(model);
    ui->tableView_2->resizeRowsToContents();
    ui->tableView_2->resizeColumnsToContents();
}

The values ​​change (when I display them in the edit field) the number of rows increases, but after trying to add a record, the tabliView_2 is cleared and only the title remains

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Seledets, 2019-01-06
@oleja1ee7

the problem was solved through tableWidget

void reportHelp::on_tableView_doubleClicked(const QModelIndex &index)
{
    ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);
    QTableWidgetItem* item = new QTableWidgetItem;
    item->setText(index.data().toString());
    item->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 0, item);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question