PDA

View Full Version : QSpinBox how to separate the numbers



aekilic
3rd September 2007, 07:45
Dear All

Could you please tell me how could we show the numbers that comes to QSpinBox, like

999,999,999.00?

jpn
3rd September 2007, 08:37
It's formatted according to current locale. I don't think one should force any specific formatting. However, if you insist, take a look at QSpinBox::textFromValue(). ;)

aekilic
3rd September 2007, 15:41
Dear jpn

What I was asking was,

you know in excel you write the number like 123456.12 and after you go to the other cell, number turns into 123,456.12. I was askin how could I make this auto for every spin?

jpn
3rd September 2007, 16:37
Do you mean something like this?


#include <QtGui>

class MyDoubleSpinBox : public QDoubleSpinBox
{
public:
MyDoubleSpinBox(QWidget* parent = 0)
: QDoubleSpinBox(parent) { }

void focusOutEvent(QFocusEvent* event)
{
QDoubleSpinBox::focusOutEvent(event);
// force reformatting
setValue(value());
}

QString textFromValue(double value) const
{
// the default implementation removes group separator characters
return QLocale(QLocale::English).toString(value, 'f', decimals());
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDoubleSpinBox spinBox;
spinBox.setRange(-999999, 999999);
spinBox.setValue(500000);
spinBox.show();
return a.exec();
}