qt - why QdoublespixBox can't show big numbers correctly -
i have problem qdoublespinxbox
i'm using qdoublespinxbox delegate in col2,
but when have big numbes 99999999999999999999999999999999
he can't show correctly , shows me 1e+19
i have used
#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 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 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); }
but nothing makes diffrent
the default implementation of qdoublespinbox::textfromvalue(double)
returns string containing value printed using
locale().tostring(value, 'f', decimals());
this never supposed return floating point representation. testing out separately, works , produces correct output:
#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
the output above correct, way, merely illustrates limited length of mantissa.
alas, maximum value not large enough show either 1e19
or 99999999999999999999999999999999.
, may problem (the commented-out assert trigger), still should not switch scientific format.
most you're not applying settings correctly spinbox, or system locale messed up. make sure test code above works correctly.
Comments
Post a Comment