「Qt Widget中文示例指南」如何实现一个旋转框(一)

发布时间:2026/7/10 14:15:55
「Qt Widget中文示例指南」如何实现一个旋转框(一) Qt 是目前最先进、最完整的跨平台C开发工具。它不仅完全实现了一次编写所有平台无差别运行更提供了几乎所有开发过程中需要用到的工具。如今Qt已被运用于超过70个行业、数千家企业支持数百万设备及应用。旋转框示例展示了如何使用Qt 中可用的许多不同类型的旋转框从简单的QSpinBox部件到更复杂的编辑器如QDateTimeEdit 部件。该示例包含一个Window类用于显示Qt中可用的不同基于旋转框的小部件。Window类定义Window类继承了QWidget并包含两个插槽用于提供交互特性class Window : public QWidget { Q_OBJECT public: Window(QWidget *parent nullptr); public slots: void changePrecision(int decimals); void setFormatString(const QString formatString); private: void createSpinBoxes(); void createDateTimeEdits(); void createDoubleSpinBoxes(); QDateTimeEdit *meetingEdit; QDoubleSpinBox *doubleSpinBox; QDoubleSpinBox *priceSpinBox; QDoubleSpinBox *scaleSpinBox; QGroupBox *spinBoxesGroup; QGroupBox *editsGroup; QGroupBox *doubleSpinBoxesGroup; QLabel *meetingLabel; QSpinBox *groupSeparatorSpinBox; QDoubleSpinBox *groupSeparatorSpinBox_d; };私有函数用于在窗口中设置每种类型的旋转框我们使用成员变量来跟踪各种小部件以便在需要时重新配置它们。Window类实现构造函数只需调用私有函数来设置示例中使用的不同类型spin box并将每个组放在一个布局中Window::Window(QWidget *parent) : QWidget(parent) { createSpinBoxes(); createDateTimeEdits(); createDoubleSpinBoxes(); QHBoxLayout *layout new QHBoxLayout; layout-addWidget(spinBoxesGroup); layout-addWidget(editsGroup); layout-addWidget(doubleSpinBoxesGroup); setLayout(layout); setWindowTitle(tr(Spin Boxes)); }我们使用布局来管理window子部件的排列并更改window标题。createSpinBoxes() 函数构造了一个QGroupBox 并在其中放置了三个QSpinBox 小部件这些小部件带有描述性标签以指示它们期望的输入类型。void Window::createSpinBoxes() { spinBoxesGroup new QGroupBox(tr(Spinboxes)); QLabel *integerLabel new QLabel(tr(Enter a value between %1 and %2:).arg(-20).arg(20)); QSpinBox *integerSpinBox new QSpinBox; integerSpinBox-setRange(-20, 20); integerSpinBox-setSingleStep(1); integerSpinBox-setValue(0);第一个旋转框显示了使用QSpinBox的最简单方法它接受从-20到20的值当前值可以通过箭头按钮或上下键增加或减少1默认值为0。第二个旋转框使用更大的步长并显示后缀以提供有关数字所代表的数据类型的更多信息QLabel *zoomLabel new QLabel(tr(Enter a zoom value between %1 and %2:).arg(0).arg(1000)); QSpinBox *zoomSpinBox new QSpinBox; zoomSpinBox-setRange(0, 1000); zoomSpinBox-setSingleStep(10); zoomSpinBox-setSuffix(%); zoomSpinBox-setSpecialValueText(tr(Automatic)); zoomSpinBox-setValue(100);此旋转框还显示一个特殊值替代为其定义的最小值这意味着它永远不会显示0%但当选择最小值时将显示自动。第三个旋转框显示了如何使用前缀QLabel *priceLabel new QLabel(tr(Enter a price between %1 and %2:).arg(0).arg(999)); QSpinBox *priceSpinBox new QSpinBox; priceSpinBox-setRange(0, 999); priceSpinBox-setSingleStep(1); priceSpinBox-setPrefix($); priceSpinBox-setValue(99);为简单起见我们展示一个带有前缀而没有后缀的旋转框也可以同时使用这两种方法。groupSeparatorSpinBox new QSpinBox; groupSeparatorSpinBox-setRange(-99999999, 99999999); groupSeparatorSpinBox-setValue(1000); groupSeparatorSpinBox-setGroupSeparatorShown(true); QCheckBox *groupSeparatorChkBox new QCheckBox; groupSeparatorChkBox-setText(tr(Show group separator)); groupSeparatorChkBox-setChecked(true); connect(groupSeparatorChkBox, QCheckBox::toggled, groupSeparatorSpinBox, QSpinBox::setGroupSeparatorShown); QLabel *hexLabel new QLabel(tr(Enter a value between %1 and %2:).arg(- QString::number(31, 16)).arg(QString::number(31, 16))); QSpinBox *hexSpinBox new QSpinBox; hexSpinBox-setRange(-31, 31); hexSpinBox-setSingleStep(1); hexSpinBox-setValue(0); hexSpinBox-setDisplayIntegerBase(16); QVBoxLayout *spinBoxLayout new QVBoxLayout; spinBoxLayout-addWidget(integerLabel); spinBoxLayout-addWidget(integerSpinBox); spinBoxLayout-addWidget(zoomLabel); spinBoxLayout-addWidget(zoomSpinBox); spinBoxLayout-addWidget(priceLabel); spinBoxLayout-addWidget(priceSpinBox); spinBoxLayout-addWidget(hexLabel); spinBoxLayout-addWidget(hexSpinBox); spinBoxLayout-addWidget(groupSeparatorChkBox); spinBoxLayout-addWidget(groupSeparatorSpinBox); spinBoxesGroup-setLayout(spinBoxLayout); }该函数的其余部分为组框设置布局并将每个小部件放置在其中。未完待续更多内容敬请期待......