翻譯|使用教程|編輯:龔雪|2024-08-20 11:14:34.810|閱讀 110 次
概述:本文將為大家介紹如何使用Qt Widget小部件如何實現一個旋轉框示例,歡迎下載最新版組件體驗~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Qt 是目前最先進、最完整的跨平臺C++開發工具。它不僅完全實現了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發過程中需要用到的工具。如今,Qt已被運用于超過70個行業、數千家企業,支持數百萬設備及應用。
旋轉框示例展示了如何使用Qt 中可用的許多不同類型的旋轉框,從簡單的QSpinBox部件到更復雜的編輯器,如 部件。
Qt技術交流群:166830288 歡迎一起進群討論
該示例包含一個Window類,用于顯示Qt中可用的不同基于旋轉框的小部件。
在上文中(點擊這里回顧>>),我們為大家介紹了實現旋轉框的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)));
可接受的時間值是使用 值定義的。
第三個旋轉框是一個小部件,它可以顯示日期和時間值,我們在其上方放置一個標簽,以指示會議允許的時間范圍。當用戶更改格式字符串時,這些小部件將被更新。
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小部件中的小數位數,當它們的小數屬性被更改時,它們中的每一個都將自動更新。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網