[SOLVED] Get the value of Qdoublespixbox implemented in delegate



  • Hello,

    i have a problem of getting a value of Qdoublespixbox imlemented in delegate

    @QWidget *customTableSellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    if(!index.isValid())
    return QStyledItemDelegate::createEditor(parent,option,index);
    QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
    doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
    doubleSpinBoxEditor->setSuffix(" D.A");
    doubleSpinBoxEditor->setDecimals(2);
    doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
    doubleSpinBoxEditor->setFrame(false);
    return doubleSpinBoxEditor;

    }@

    and i have added the delegate to my qtablwidget

    @ m_tableDelegate = new customTableSellDelegate(this);
    ui->tableWidget->setItemDelegate(m_tableDelegate);@

    after that when the user make some change in qtablewidget

    i tried to get a value from qtablewidget

    @double qty = ui->tableWidget->item(row,2)->text().toDouble(); @

    but it return 0 alway, even thought i write 25 , so what's the problem


  • Lifetime Qt Champion

    Hi,

    What does ui->tableWidget->item(row,2)->text() return ?



  • i have writed 25 in qtablewidget but

    but ui->tableWidget->item(row,2)->text()

    returns "Ƀ砀Ƀሀ䑫\002簓Ʉ\000㬷\027Ԁȟ婟䭎儶敒瑣ㅆ椱瑮牥敳瑣摥剅卋_㖊\002ꬁɃ똀" ( using debugger )


  • Lifetime Qt Champion

    That doesn't look like a number.

    Did you completely implement the delegate ?



  • Yes this is my custom delegate

    @#include "customtableselldelegate.h"
    #include <QDoubleSpinBox>
    #include <QComboBox>
    #include <QDebug>
    customTableSellDelegate::customTableSellDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
    {
    }

    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);
        comboboxEditor->addItem("Item1");
        comboboxEditor->addItem("Item2");
        comboboxEditor->addItem("Item3");
        comboboxEditor->addItem("Item4");
        comboboxEditor->addItem("Item5");
        comboboxEditor->addItem("Item6");
    
    
        return comboboxEditor;
    
    }
    // col3
    else if(col ==2 || col ==3 || col ==4 || col == 5 || col == 6 || col == 7)
    {
        QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
        doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
        doubleSpinBoxEditor->setSuffix(" D.A");
        doubleSpinBoxEditor->setDecimals(2);
        doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
        doubleSpinBoxEditor->setFrame(false);
        return doubleSpinBoxEditor;
    
    
    }else{
        return QStyledItemDelegate::createEditor(parent,option,index);
    }
    

    }

    void customTableSellDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
    if(!index.isValid())
    return QStyledItemDelegate::setEditorData(editor,index);

    int col= index.column();
    
    if(col == 1)
    {
        QString data = index.model()->data(index,Qt::DisplayRole).toString();
        QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
    
        comboboxEditor->setCurrentText(data);
    }
    
    else if(col ==2 || col ==3 || col ==4 || col == 5 || col == 6 || col == 7)
    {
        double data = index.model()->data(index,Qt::DisplayRole).toDouble();
        QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
        doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
        doubleSpinBoxEditor->setSuffix(" D.A");
        doubleSpinBoxEditor->setDecimals(2);
        doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
        doubleSpinBoxEditor->setFrame(false);
        doubleSpinBoxEditor->setValue(data);
    
    }else{
        QStyledItemDelegate::setEditorData(editor,index);
    }
    

    }

    void customTableSellDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
    if(!index.isValid())
    return QStyledItemDelegate::setModelData(editor,model,index);

    int col= index.column();
    
    if(col == 1)
    {
        QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
    
        model->setData(index,comboboxEditor->currentText(),Qt::EditRole);
    
        emit unlockRow(index);
    
    
    }
    // col3
    else if(col ==2 || col ==3 || col ==4 || col == 6 || col == 7)
    {
        QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
        doubleSpinBoxEditor->setInputMethodHints(Qt::ImhFormattedNumbersOnly);
        doubleSpinBoxEditor->setRange(0.000000,999999999.000000);
        doubleSpinBoxEditor->setSuffix(" D.A");
        doubleSpinBoxEditor->setDecimals(2);
        doubleSpinBoxEditor->setFrame(false);
        model->setData(index,doubleSpinBoxEditor->textFromValue(doubleSpinBoxEditor->value()),Qt::EditRole);
        if(col == 2 || col == 3)
        {
            emit qtyPriceDataChanged(index);
        }
    }else{
        QStyledItemDelegate::setModelData(editor,model,index);}
    

    }

    void customTableSellDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    editor->setGeometry(option.rect);

    }

    @


  • Lifetime Qt Champion

    Why are you setting the double spin box properties in each function ? You should only do that when creating the editor.

    Why ?
    @model->setData(index,doubleSpinBoxEditor->textFromValue(doubleSpinBoxEditor->value()),Qt::EditRole)@

    rather :

    @model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole)@

    As a side note, in setModelData, you don't handle column 5.

    Have a look at the Spin Box delegate example



  • [quote author="SGaist" date="1380916344"]Why are you setting the double spin box properties in each function ? You should only do that when creating the editor.

    Why ?
    @model->setData(index,doubleSpinBoxEditor->textFromValue(doubleSpinBoxEditor->value()),Qt::EditRole)@

    rather :

    @model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole)@

    As a side note, in setModelData, you don't handle column 5.

    Have a look at the Spin Box delegate example[/quote]

    Yes, you're right , all mostly done , thanks a lot .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.