PDA

View Full Version : Getting / setting data on QTableWidgets



orfner
28th October 2011, 22:58
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);
}

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);
}

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:


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);
}

What am I doing wrong?

Santosh Reddy
30th October 2011, 02:33
QTableWidgetItem (http://doc.qt.nokia.com/latest/qtablewidgetitem.html) * QTableWidget::itemAt ( const QPoint (http://doc.qt.nokia.com/latest/qpoint.html) & point ) const

QTableWidgetItem (http://doc.qt.nokia.com/latest/qtablewidgetitem.html) * QTableWidget::itemAt ( int ax, int ay ) const

for both the functions the parameter is point (in table widget's coordinate system), this is not row & column. This is the reason for empty in the text().

Try using this
QTableWidgetItem (http://doc.qt.nokia.com/latest/qtablewidgetitem.html) * QTableWidget::item ( int row, int column ) const

Use the second method.
First method should also work if Qt::UserRole is changed to Qt:: DataRole