0

我对 QdoublespinxBox 有疑问

我在 col2 中使用 Qdoublespinxbox 作为代表,

但是当我有像 999999999999999999999999999999999 这样的大数字时

他无法正确显示,他向我显示 1e+19

我用过这个

    #include "customtableselldelegate.h"
#include <QDoubleSpinBox>
#include <QComboBox>
#include <QDebug>
#include <databasemananger.h>
#include <QLocale>
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 == 0)
    {
        DataBaseMananger dbMgr;

        QSqlQueryModel *queryModel = new QSqlQueryModel(parent);

        queryModel->setQuery("SELECT articleDesignation FROM articles");

        QComboBox *comboboxEditor = new QComboBox(parent);

        comboboxEditor->setModel(queryModel);
        //comboboxEditor->setEditable(true);


        return comboboxEditor;

    }
    else if(col ==1 || col ==2 || col ==3 ||  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 == 0)
    {
        QString data = index.model()->data(index,Qt::DisplayRole).toString();
        QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);

        comboboxEditor->setItemText(comboboxEditor->currentIndex(),data);
    }

    else if(col ==1 || col ==2 || col ==3 ||  col == 5 || col == 6 || col == 7)
    {
        double data = index.model()->data(index,Qt::DisplayRole).toDouble();
        QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
        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 == 0)
    {
        QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);

        model->setData(index,comboboxEditor->currentText(),Qt::EditRole);

        emit unlockRow(index);


    }

    else if(col ==1 ||col ==2 || col ==3 ||  col == 5 || col == 6 || col == 7)
    {
        QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
        model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole);

        if(col == 1 || col == 2)
        {
            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);

}

void customTableSellDelegate::emitUnlockRow(QString str, QModelIndex index)
{
    emit unlockRow(index);

}


#include "customtableselldelegate.h"
#include <QDoubleSpinBox>
#include <QComboBox>
#include <QDebug>
#include <databasemananger.h>
#include <QLocale>
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 == 0)
    {
        DataBaseMananger dbMgr;

        QSqlQueryModel *queryModel = new QSqlQueryModel(parent);

        queryModel->setQuery("SELECT articleDesignation FROM articles");

        QComboBox *comboboxEditor = new QComboBox(parent);

        comboboxEditor->setModel(queryModel);
        //comboboxEditor->setEditable(true);


        return comboboxEditor;

    }
    else if(col ==1 || col ==2 || col ==3 ||  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 == 0)
    {
        QString data = index.model()->data(index,Qt::DisplayRole).toString();
        QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);

        comboboxEditor->setItemText(comboboxEditor->currentIndex(),data);
    }

    else if(col ==1 || col ==2 || col ==3 ||  col == 5 || col == 6 || col == 7)
    {
        double data = index.model()->data(index,Qt::DisplayRole).toDouble();
        QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
        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 == 0)
    {
        QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);

        model->setData(index,comboboxEditor->currentText(),Qt::EditRole);

        emit unlockRow(index);


    }

    else if(col ==1 ||col ==2 || col ==3 ||  col == 5 || col == 6 || col == 7)
    {
        QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
        model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole);

        if(col == 1 || col == 2)
        {
            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);

}

void customTableSellDelegate::emitUnlockRow(QString str, QModelIndex index)
{
    emit unlockRow(index);

}

但真的没有什么不同

4

1 回答 1

1

默认实现QDoubleSpinBox::textFromValue(double)返回一个字符串,其中包含使用打印的

locale().toString(value, 'f', decimals());

除了浮点表示之外,这永远不应该返回任何东西。单独测试它,它肯定可以工作并产生正确的输出:

#include <QTextStream>
#include <QLocale
int main(int, char **)
{
    QTextStream out(stdout);
    QLocale sysLoc = QLocale::system();
    double value = 99999999999999999999999999999999.;
    double max = 999999999999999999.99;
    int decimals = 2;
    //Q_ASSERT(value < max);
    QString str = sysLoc.toString(value, 'f', decimals);
    str.remove(sysLoc.groupSeparator());
    out << str << endl;
    return 0;
}

100000000000000005366162204393472.00

上面的输出是正确的,顺便说一下,它只是说明了尾数的有限长度。

唉,你的最大值不足以显示1e19or 99999999999999999999999999999999.,所以这可能是你的问题(注释掉的断言会触发),但它仍然不应该切换到科学格式。

很可能您没有将设置正确地应用到您的 spinbox,或者您的系统区域设置被搞砸了。确保上面的测试代码正常工作。

于 2013-10-07T11:02:35.253 回答