2013-10-07 93 views
0

我有QdoublespinxBox爲什麼QdoublespixBox不能顯示大的數字正確

問題

我使用Qdoublespinxbox在COL2代表,

,但是當我有大numbes像99999999999999999999999999999999

他不能正確顯示,他給我看了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); 

} 

但確實沒有什麼使得QDoubleSpinBox::textFromValue(double)一個不同勢

回答

1

默認實現返回一個包含字符串值印刷using

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

上面的輸出是正確的,順便說一下,它僅示出尾數的有限長度。

唉,你的最大值不夠大,顯示無論是1e1999999999999999999999999999999999.,所以,可能是你的問題(被註釋掉的斷言會觸發),但仍不能切換到科學的格式。

很可能你沒有正確地將你的設置應用到你的紡紗箱,或者你的系統區域設置混亂了。確保上述測試代碼正常工作。

+0

我使用自定義委託,真的我不知道如何編碼 – advseo32

+0

@ advseo32:顯示您的代碼 - 編輯問題。自定義代表什麼? QML視圖?基於QWidget的視圖? –

+0

我使用簡單的qt程序的自定義代理 – advseo32

相關問題