0

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

像这样:Num(默认)| 项目(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);
    }

}

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

4

1 回答 1

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

                      }

        }
    }
于 2013-09-27T17:07:27.517 回答