1

I have QTableWidget connected with totale() slot and itemChanged(QTableWidgetItem *) signal, in my table I am tracking user change on column 3 and 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));
        }
    }
}

My problem if I make change on column 5, it works fine, but when I made change on column 3 I get segmentation fault.

4

1 回答 1

1

尝试在添加新行时关闭 cellChanged(int,int) 信号(断开并连接它)。这样,您将不会从未填充的单元格中读取

于 2013-06-25T14:02:01.873 回答