How to change QDoubleSpinBox number format



  • Hello, i have implemented a custom delegate with QDoubleSpinBox and Qcombobox

    but the format of the number in QDoubleSpinBox not good for me , what i can do to make it look like

    " 999 999 999 9999 999 999 $",

    her is my implementation so far

    @#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);
    
        if(col == 2 || col == 3)
        {
            connect(doubleSpinBoxEditor,SIGNAL(valueChanged(double)),this,SIGNAL(onDoubleSpindBoxQtyPrixChanged(double)));
    
        }
    
        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);
    }
    // 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);
    
    }else{
        QStyledItemDelegate::setModelData(editor,model,index);}
    

    }

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

    }

    void customTableSellDelegate::testSlot(double number)
    {
    qDebug() << "le nombre est: " << number ;

    }
    @



  • really, how i can control that
    ?


  • Moderators

    shoudln't it be enough to just subclass QDoubleSpinBox and reimplement QDoubleSpinBox::textFromValue() method? (The method is virtual).


Log in to reply
 

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