スピンボックスの例
Spin Boxes のサンプルでは、シンプルなQSpinBox ウィジェットからQDateTimeEdit ウィジェットのような複雑なエディタまで、Qt で利用可能な様々なタイプのスピンボックスの使用方法を示します。
この例では、Qt で利用可能な様々なスピンボック スベースのウィジェットを表示するために使用される、Window
クラスで構成されています。
ウィンドウクラスの定義
Window
クラスはQWidget を継承し、インタラクティブな機能を提供するために使用される 2 つのスロットを含んでいます:
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::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")); }
レイアウトを使用して、ウィンドウの子ウィジェットの配置を管理し、ウィンドウのタイトルを変更します。
createSpinBoxes()
関数は、QGroupBox を構築し、その中に 3 つの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 までの値を受け入れ、現在の値は矢印ボタンまたはUp とDown キーで 1 ずつ増減でき、デフォルト値は 0 です。
2番目のスピンボックスは、より大きなステップサイズを使用し、数値が表すデータの種類に関する詳細情報を提供する接尾辞を表示します:
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);
また、このスピンボックスは最小値の代わりにspecial value を表示します。つまり、0% は表示されませんが、最小値が選択されるとAutomatic が表示されます。
3つ目のスピンボックスは接頭辞の使い方を示しています:
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); }
残りの関数は、グループボックスのレイアウトを設定し、各ウィジェットをその中に配置します。
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 の値で指定された範囲内の日付を入力できます。矢印ボタン、Up 、Down キーを使用して、カーソルが関連するセクションにあるときに、年、月、日の値を増減することができます。
2番目のスピンボックスは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 の値を使用して定義されます。
3つ目のスピンボックスは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()
関数は、倍精度浮動小数点数の入力に使用される 3 つのスピンボックスを構築します:
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 ウィジェットが構築される前に、小数点以下の桁数を制御するためのスピンボックスを作成します。デフォルトでは、以下のスピンボックスでは小数点以下2桁のみが表示されます。各スピンボックスは、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);
ただし、このスピンボックスでは整数以外の値を入力することもできます。
2番目のスピンボックスは接尾辞を表示し、最小値の代わりに特別な値を表示します:
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);
3番目のスピンボックスは接尾辞の代わりに接頭辞を表示します:
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 の各ウィジェットの小数点以下の桁数を指定するために、シグナルから供給された整数を使用します。それぞれのdecimals プロパティが変更されると、自動的に更新されます。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.