qt-C++笔记之QSpinBox控件
code review!
文章目录
- qt-C++笔记之QSpinBox控件
- 1.运行
- 2.main.cpp
- 3.main.pro
- 4.《Qt6 C++开发指南》:4.4 QSpinBox 和QDoubleSpinBox
1.运行
2.main.cpp
#include <QApplication>
#include <QSpinBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>int main(int argc, char *argv[]) {QApplication app(argc, argv);// 创建窗口和布局QWidget window;QVBoxLayout *layout = new QVBoxLayout(&window);// 创建QSpinBoxQSpinBox *spinBox = new QSpinBox;// 设置范围spinBox->setRange(0, 100);// 设置单步大小spinBox->setSingleStep(1);// 设置前缀和后缀spinBox->setPrefix("$");spinBox->setSuffix(" USD");// 设置特殊文本显示spinBox->setSpecialValueText("None");// 启用/禁用循环spinBox->setWrapping(true);// 设置初始值spinBox->setValue(50);// 创建一个打印当前值的按钮QPushButton *printButton = new QPushButton("Print Value");// 连接信号和槽QObject::connect(printButton, &QPushButton::clicked, [spinBox]() {qDebug() << "The value of spinBox is:" << spinBox->value();});// 添加到布局layout->addWidget(spinBox);layout->addWidget(printButton);// 显示窗口window.setLayout(layout);window.show();return app.exec();
}
3.main.pro
QT += widgets core
TARGET = qt_test
TEMPLATE = app
SOURCES += main.cpp