我已将QTableWidget与totale()
广告连接和itemChanged(QTableWidgetItem *)信号相关联,在我的表格中,我正在跟踪第3列和第5列的用户更改。
void Store::totale(QTableWidgetItem *x)
{
int row = x->row();
float sum = 0;
float ttl = 0;
QTableWidgetItem *item = NULL;
if (ui->storeTab->currentIndex() == 1)
{
if(x->column() == 5)
{
item = new QTableWidgetItem;
sum = ui->itemsTableWidget->item(row, 3)->text().toFloat() * x->data(0).toFloat();
item->setText(QString::number(sum, 'f', 2));
ui->itemsTableWidget->setItem(x->row(), 6, item);
sum = 0;
for (int i = 0; i < ui->itemsTableWidget->rowCount(); ++i)
{
ttl += ui->itemsTableWidget->item(i, 5)->text().toFloat();
sum += ui->itemsTableWidget->item(i, 6)->text().toFloat();
}
ui->totaleBill0->setText(QString::number(sum, 'f', 2));
ui->ttlBill0->setText(QString::number(ttl, 'f', 2));
}
else if(x->column() == 3)
{
item = new QTableWidgetItem;
sum = x->data(Qt::DisplayRole).toFloat() * ui->itemsTableWidget->item(row, 5)->text().toFloat();
item->setText(QString::number(sum, 'f', 2));
ui->itemsTableWidget->setItem(x->row(), 6, item);
sum = 0;
for (int i = 0; i < ui->itemsTableWidget->rowCount(); ++i)
{
ttl += ui->itemsTableWidget->item(i, 5)->text().toFloat();
sum += ui->itemsTableWidget->item(i, 6)->text().toFloat();
}
ui->totaleBill0->setText(QString::number(sum, 'f', 2));
ui->ttlBill0->setText(QString::number(ttl, 'f', 2));
}
}
}
我的问题是如果我在第5列进行更改,它工作正常,但当我在第3列进行更改时,我得到分段错误。
答案 0 :(得分:1)
尝试在添加新行时断开cellChanged(int,int)信号(断开连接并连接它)。通过这种方式,您将无法从未填充的单元格中读取