Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change QDoubleSpinBox number format
QtWS25 Last Chance

How to change QDoubleSpinBox number format

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    advseo32
    wrote on last edited by
    #1

    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 ;

    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      advseo32
      wrote on last edited by
      #2

      really, how i can control that
      ?

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved