1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <QDoubleSpinBox> #include <QVBoxLayout> #include <QLabel> #include <QDebug> #include <qapplication.h>
void setupDoubleSpinBoxDemo(QWidget *parent) { QVBoxLayout *layout = new QVBoxLayout(parent);
QLabel *label1 = new QLabel("商品单价设置 (0.00 - 999.99):", parent); QDoubleSpinBox *priceBox = new QDoubleSpinBox(parent);
priceBox->setRange(0.00, 999.99); priceBox->setDecimals(2); priceBox->setSingleStep(0.5); priceBox->setPrefix("$ "); priceBox->setValue(9.9);
QLabel *label2 = new QLabel("实验参数 (高精度 4 位小数):", parent); QDoubleSpinBox *paramBox = new QDoubleSpinBox(parent); paramBox->setRange(-1.0, 1.0); paramBox->setDecimals(4); paramBox->setSingleStep(0.0001); paramBox->setSuffix(" unit");
QLabel *displayLabel = new QLabel("当前价格:$ 9.90", parent);
QObject::connect(priceBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [displayLabel](double newValue) { displayLabel->setText(QString("当前价格:$ %1").arg(newValue, 0, 'f', 2)); });
layout->addWidget(label1); layout->addWidget(priceBox); layout->addWidget(label2); layout->addWidget(paramBox); layout->addWidget(displayLabel); layout->addStretch(); } int main(int argc, char *argv[]) { QApplication a(argc, argv);
QWidget w; w.setWindowTitle("QLineEdit 深度测试"); w.resize(400, 300);
setupDoubleSpinBoxDemo(&w);
w.show(); return a.exec(); }
|