在 QTableWidgets 上获取和设置数据时出现问题
我有两个 QTableWidget,我正在尝试同步它们作为练习,以找出如何获取/设置数据。
我当前的代码是:
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)
{
QString value = ui->tableWidget_2->itemAt(row,column)->text();
ui->tableWidget->itemAt(row,column)->setText(value);
}
不管怎样,从 QTableWidgetItem::text() 返回的 QString 是一个空字符串。无论我尝试编辑之前是否有任何文本,都会发生这种情况。
不确定这是否重要,但这就是我初始化表的方式:
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);
}
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
void QTableWidgetItem::setData ( int role, const QVariant & value )
你也许应该这样尝试:
并使用这样的方式取回数据:
void QTableWidgetItem::setData ( int role, const QVariant & value )
You maybe should try like this:
and get back the data using like this: