Reputation: 1
I have 4 doublespinboxes in my Window. I want to display the values of fixed width in this doublespinbox. For e.g my doublespinbox range is 0 to 100.00 I want to display the values in the format of 000.00 always.So though the value is 8 it should get displayed as 008.00 in my doublespinbox . Similarly I want highlight each digit in my doublespinbox during editing the values . How can I do the same? . The width/range varies for all the spinboxes. Can somebody help me.
Upvotes: 0
Views: 796
Reputation: 3645
As I've said in comment to @asclepix post you need to reimplement textFromValue
. This snippet works fine for me.
class MyDoubleSpinBox : public QDoubleSpinBox
{
public:
explicit MyDoubleSpinBox(QWidget *parent = 0) : QDoubleSpinBox(parent) {
setMaximum(999.99);
}
QString textFromValue(double val) const {
const int width = 6; // length of whole number in symbols '000.00'
const int precision = 2; // after separator
// rightJustified to add leading zeroes
return QLocale().toString(val, 'f', precision).rightJustified(width, '0');
}
};
Upvotes: 1
Reputation: 8061
I suppose you have to use setDecimals to to force the trailing zeros and setPrefix for the leading ones. The problem is that you have to change the prefix depending on the value of the doublespinbox. The simple way is to connect a slot to the valueChanged signal and do the job there. The less simple way is to subclass the doublespinBox, but I don't know what to reimplement.
Upvotes: 0