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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include "NumberSpinBox.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QSpinBox>
#include <QVBoxLayout>
#include <QWidget>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr)
: QMainWindow(parent) {
QWidget* w = new QWidget(this);
QVBoxLayout* l = new QVBoxLayout();
NumberSpinBox* nb = new NumberSpinBox(5, w);
{
QHBoxLayout* h = new QHBoxLayout();
auto* lb = new QLabel(QStringLiteral("Prefix:"), this);
auto* le = new QLineEdit(this);
h->addWidget(lb);
h->addWidget(le);
l->addLayout(h);
connect(le, &QLineEdit::textChanged, nb, &NumberSpinBox::setPrefix);
}
{
QHBoxLayout* h = new QHBoxLayout();
auto* lb = new QLabel(QStringLiteral("Suffix:"), this);
auto* le = new QLineEdit(this);
h->addWidget(lb);
h->addWidget(le);
l->addLayout(h);
connect(le, &QLineEdit::textChanged, nb, &NumberSpinBox::setSuffix);
}
{
QHBoxLayout* h = new QHBoxLayout();
auto* lb = new QLabel(QStringLiteral("Min:"), this);
auto* sb = new QSpinBox(this);
sb->setMinimum(-1e6);
sb->setMaximum(1e6);
sb->setValue(-10);
nb->setMinimum(-10);
h->addWidget(lb);
h->addWidget(sb);
l->addLayout(h);
connect(sb, QOverload<int>::of(&QSpinBox::valueChanged), [nb](int min) {
nb->setMinimum(min);
});
}
{
QHBoxLayout* h = new QHBoxLayout();
auto* lb = new QLabel(QStringLiteral("Max:"), this);
auto* sb = new QSpinBox(this);
sb->setMinimum(-1e6);
sb->setMaximum(1e6);
sb->setValue(10);
nb->setMaximum(10);
h->addWidget(lb);
h->addWidget(sb);
l->addLayout(h);
connect(sb, QOverload<int>::of(&QSpinBox::valueChanged), [nb](int max) {
nb->setMaximum(max);
});
}
QLineEdit* le = new QLineEdit(w);
connect(nb, QOverload<double>::of(&NumberSpinBox::valueChanged), [le](double value) {
le->setText(QString::number(value));
});
l->addWidget(nb);
l->addWidget(le);
w->setLayout(l);
setCentralWidget(w);
}
private:
};
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "NumberSpinBoxMain.moc"
|