在Qtablewidget中计算小计

时间:2013-09-27 10:55:14

标签: delegates qtablewidget subtotal

我有Qtablewidget我已经为该表实现了委托

像这样:Num(默认)| item(Qcombobox)| -qty-(QdoubleSpinBox)|价格(QdoubleSpinBox)|小计(QdoubleSpinBox)

我想使用委托计算每一行的小计(数量*价格),但实际上我不知道如何从委托中发出数据

我从我的自定义代表

尝试了这个
QWidget *customTableSellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(!index.isValid())
        return QStyledItemDelegate::createEditor(parent,option,index);

    int col= index.column();

    if(col == 1)
    {
        QComboBox *comboboxEditor = new QComboBox(parent);
        return comboboxEditor;
    }
    // col3
    else if(col ==2 || col ==3 || col ==4 || col == 5 || col == 6 || col == 7)
    {
        QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
        doubleSpinBoxEditor->setInputMethodHints(Qt::ImhFormattedNumbersOnly);
        doubleSpinBoxEditor->setRange(0.000000,999999999);
        doubleSpinBoxEditor->setSuffix(" D.A");
        doubleSpinBoxEditor->setDecimals(2);
        doubleSpinBoxEditor->setFrame(false);

        if(col == 2 || col == 3)
        {
            connect(doubleSpinBoxEditor,SIGNAL(valueChanged(double)),this,SLOT(testSlot(double)));
        }

        return doubleSpinBoxEditor;
    }else{
        return QStyledItemDelegate::createEditor(parent,option,index);
    }

}

那么,最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

在认为我得到解决方案后,她就是代码

注意:这是我的逻辑,你可以根据需要改变它

   QTableWidgetItem *item= NULL;
    for(int row=0; row < ui->tableWidget->rowCount(); row++)
    {
        for (int col=0; col< ui->tableWidget->columnCount(); col++)
        {

            item = new QTableWidgetItem;
            ui->tableWidget->setItem(row,col,item);


        }
    }
    for(int row =0; row < ui->tableWidget->rowCount(); row++)
    {
        for(int col=0; col < ui->tableWidget->columnCount(); col++)
        {
            if(row != 0)
                ui->tableWidget->item(row,col)->setFlags(Qt::NoItemFlags);
            else{
                if(col == 0 || col == 4 || col == 5 || col == 7 || col == 6)
                    ui->tableWidget->item(row,col)->setFlags(Qt::NoItemFlags);

                      }

        }
    }