「Qt Widget中文示例指南」如何实现一个旋转框(二)
Qt是目前最先进、最完整的跨平台C开发工具。它不仅完全实现了一次编写所有平台无差别运行更提供了几乎所有开发过程中需要用到的工具。如今Qt已被运用于超过70个行业、数千家企业支持数百万设备及应用。旋转框示例展示了如何使用Qt中可用的许多不同类型的旋转框从简单的QSpinBox部件到更复杂的编辑器如QDateTimeEdit 部件。该示例包含一个Window类用于显示Qt中可用的不同基于旋转框的小部件。在上文中我们为大家介绍了实现旋转框的Window类定义和部分Window类实现内容本文将继续讲解如何实现Window类。Window类实现createDateTimeEdits()函数构造另一个组框其中包含用于编辑日期和时间的旋转框。void Window::createDateTimeEdits() { editsGroup new QGroupBox(tr(Date and time spin boxes)); QLabel *dateLabel new QLabel; QDateEdit *dateEdit new QDateEdit(QDate::currentDate()); dateEdit-setDateRange(QDate(2005, 1, 1), QDate(2010, 12, 31)); dateLabel-setText(tr(Appointment date (between %0 and %1):) .arg(dateEdit-minimumDate().toString(Qt::ISODate)) .arg(dateEdit-maximumDate().toString(Qt::ISODate)));第一个旋转框是QDateEdit小部件它能够接受使用QDate值指定的给定范围内的日期当光标位于相关区域时可以使用箭头按钮和上下键来增加和减少年、月和日的值。第二个旋转框是QTimeEdit小部件QLabel *timeLabel new QLabel; QTimeEdit *timeEdit new QTimeEdit(QTime::currentTime()); timeEdit-setTimeRange(QTime(9, 0, 0, 0), QTime(16, 30, 0, 0)); timeLabel-setText(tr(Appointment time (between %0 and %1):) .arg(timeEdit-minimumTime().toString(Qt::ISODate)) .arg(timeEdit-maximumTime().toString(Qt::ISODate)));可接受的时间值是使用QTime 值定义的。第三个旋转框是一个QDateTimeEdit小部件它可以显示日期和时间值我们在其上方放置一个标签以指示会议允许的时间范围。当用户更改格式字符串时这些小部件将被更新。meetingLabel new QLabel; meetingEdit new QDateTimeEdit(QDateTime::currentDateTime());日期时间编辑器使用的格式字符串(也显示在标签显示的字符串中)是从组合框中的一组字符串中选择的QLabel *formatLabel new QLabel(tr(Format string for the meeting date and time:)); QComboBox *formatComboBox new QComboBox; formatComboBox-addItem(yyyy-MM-dd hh:mm:ss (zzz ms)); formatComboBox-addItem(hh:mm:ss MM/dd/yyyy); formatComboBox-addItem(hh:mm:ss dd/MM/yyyy); formatComboBox-addItem(hh:mm:ss); formatComboBox-addItem(hh:mm ap); connect(formatComboBox, QComboBox::textActivated, this, Window::setFormatString);来自这个组合框的信号被连接到Window类中的一个槽(稍后会显示)。QVBoxLayout *editsLayout new QVBoxLayout; editsLayout-addWidget(dateLabel); editsLayout-addWidget(dateEdit); editsLayout-addWidget(timeLabel); editsLayout-addWidget(timeEdit); editsLayout-addWidget(meetingLabel); editsLayout-addWidget(meetingEdit); editsLayout-addWidget(formatLabel); editsLayout-addWidget(formatComboBox); editsGroup-setLayout(editsLayout); }组框的每个子部件都放置在一个布局中。每当用户在组合框中选择新的格式字符串时就调用setFormatString()槽。QDateTimeEdit小部件的显示格式是使用信号传递的原始字符串设置的void Window::setFormatString(const QString formatString) { meetingEdit-setDisplayFormat(formatString);根据小部件中可见的部分我们设置一个新的日期或时间范围并更新相关的标签为用户提供相关信息if (meetingEdit-displayedSections() QDateTimeEdit::DateSections_Mask) { meetingEdit-setDateRange(QDate(2004, 11, 1), QDate(2005, 11, 30)); meetingLabel-setText(tr(Meeting date (between %0 and %1):) .arg(meetingEdit-minimumDate().toString(Qt::ISODate)) .arg(meetingEdit-maximumDate().toString(Qt::ISODate))); } else { meetingEdit-setTimeRange(QTime(0, 7, 20, 0), QTime(21, 0, 0, 0)); meetingLabel-setText(tr(Meeting time (between %0 and %1):) .arg(meetingEdit-minimumTime().toString(Qt::ISODate)) .arg(meetingEdit-maximumTime().toString(Qt::ISODate))); } }当格式字符串改变时将会有一个适当的标签和输入小部件用于日期、时间或两种类型的输入。createDoubleSpinBoxes()函数构造了三个旋转盒用于输入双精度浮点数void Window::createDoubleSpinBoxes() { doubleSpinBoxesGroup new QGroupBox(tr(Double precision spinboxes)); QLabel *precisionLabel new QLabel(tr(Number of decimal places to show:)); QSpinBox *precisionSpinBox new QSpinBox; precisionSpinBox-setRange(0, 100); precisionSpinBox-setValue(2);在构造QDoubleSpinBox小部件之前我们创建一个旋转框来控制它们显示的小数位数。默认情况下下面的旋转框中只显示两个小数点每个旋转框都相当于createSpinBoxes()函数创建的组中的一个旋转框。第一个双旋转框显示了一个基本的双精度旋转框它的范围、步长和默认值与createSpinBoxes()函数中的第一个旋转框相同QLabel *doubleLabel new QLabel(tr(Enter a value between %1 and %2:).arg(-20).arg(20)); doubleSpinBox new QDoubleSpinBox; doubleSpinBox-setRange(-20.0, 20.0); doubleSpinBox-setSingleStep(1.0); doubleSpinBox-setValue(0.0);但是这个旋转框也允许输入非整数值。第二个旋转框显示一个后缀并显示一个特殊值而不是最小值QLabel *scaleLabel new QLabel(tr(Enter a scale factor between %1 and %2:).arg(0).arg(1000.0)); scaleSpinBox new QDoubleSpinBox; scaleSpinBox-setRange(0.0, 1000.0); scaleSpinBox-setSingleStep(10.0); scaleSpinBox-setSuffix(%); scaleSpinBox-setSpecialValueText(tr(No scaling)); scaleSpinBox-setValue(100.0);第三个旋转框显示前缀替代后缀QLabel *priceLabel new QLabel(tr(Enter a price between %1 and %2:).arg(0).arg(1000)); priceSpinBox new QDoubleSpinBox; priceSpinBox-setRange(0.0, 1000.0); priceSpinBox-setSingleStep(1.0); priceSpinBox-setPrefix($); priceSpinBox-setValue(99.99); connect(precisionSpinBox, QSpinBox::valueChanged,我们将指定精度的QSpinBox小部件连接到Window类中的一个槽。QVBoxLayout *spinBoxLayout new QVBoxLayout; spinBoxLayout-addWidget(precisionLabel); spinBoxLayout-addWidget(precisionSpinBox); spinBoxLayout-addWidget(doubleLabel); spinBoxLayout-addWidget(doubleSpinBox); spinBoxLayout-addWidget(scaleLabel); spinBoxLayout-addWidget(scaleSpinBox); spinBoxLayout-addWidget(priceLabel); spinBoxLayout-addWidget(priceSpinBox); spinBoxLayout-addWidget(groupSeparatorChkBox); spinBoxLayout-addWidget(groupSeparatorSpinBox_d); doubleSpinBoxesGroup-setLayout(spinBoxLayout); }该函数的其余部分将每个小部件放置到组框的布局中。当用户改变精密旋转框中的值时调用changePrecision()槽void Window::changePrecision(int decimals) { doubleSpinBox-setDecimals(decimals); scaleSpinBox-setDecimals(decimals); priceSpinBox-setDecimals(decimals); }该函数仅使用信号提供的整数来指定每个QDoubleSpinBox小部件中的小数位数当它们的小数属性被更改时它们中的每一个都将自动更新。