I have two QTableWidgets and I'm trying to synchronize them as an exercise to figure out how to get / set data.
The current code I have is:
void MainWindow::on_tableWidget_2_cellChanged(int row, int column)
{
double value = ui->tableWidget_2->itemAt(row,column)->text().toDouble();
ui->tableWidget->itemAt(row,column)->setData(Qt::UserRole, value);
}
void MainWindow::on_tableWidget_2_cellChanged(int row, int column)
{
double value = ui->tableWidget_2->itemAt(row,column)->text().toDouble();
ui->tableWidget->itemAt(row,column)->setData(Qt::UserRole, value);
}
To copy to clipboard, switch view to plain text mode
I have also tried:
void MainWindow::on_tableWidget_2_cellChanged(int row, int column)
{
QString value
= ui
->tableWidget_2
->itemAt
(row,column
)->text
();
ui->tableWidget->itemAt(row,column)->setText(value);
}
void MainWindow::on_tableWidget_2_cellChanged(int row, int column)
{
QString value = ui->tableWidget_2->itemAt(row,column)->text();
ui->tableWidget->itemAt(row,column)->setText(value);
}
To copy to clipboard, switch view to plain text mode
No matter what, the QString returned from QTableWidgetItem::text() is an empty string. This happens regardless of whether or not there was any text before I attempted editing.
Not sure if it matters, but this is how I initialize the table:
for(int i = 0; i < 5; i++)
{
ui->tableWidget->insertRow(i);
ui->tableWidget_2->insertRow(i);
ui->tableWidget->setItem(i,0,tableItem);
ui->tableWidget->setItem(i,1,tableItem);
ui->tableWidget_2->setItem(i,0,tableItem);
ui->tableWidget_2->setItem(i,1,tableItem);
}
QTableWidgetItem * tableItem;
for(int i = 0; i < 5; i++)
{
ui->tableWidget->insertRow(i);
ui->tableWidget_2->insertRow(i);
tableItem = new QTableWidgetItem();
ui->tableWidget->setItem(i,0,tableItem);
tableItem = new QTableWidgetItem();
ui->tableWidget->setItem(i,1,tableItem);
tableItem = new QTableWidgetItem();
ui->tableWidget_2->setItem(i,0,tableItem);
tableItem = new QTableWidgetItem();
ui->tableWidget_2->setItem(i,1,tableItem);
}
To copy to clipboard, switch view to plain text mode
What am I doing wrong?
Bookmarks