翻譯|使用教程|編輯:龔雪|2024-01-10 10:24:28.230|閱讀 214 次
概述:本文將為大家介紹如何使用Qt Widget小部件中的QCalendarWidget來(lái)實(shí)現(xiàn)日歷功能,歡迎下載最新版組件體驗(yàn)~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Qt 是目前最先進(jìn)、最完整的跨平臺(tái)C++開發(fā)工具。它不僅完全實(shí)現(xiàn)了一次編寫,所有平臺(tái)無(wú)差別運(yùn)行,更提供了幾乎所有開發(fā)過程中需要用到的工具。如今,Qt已被運(yùn)用于超過70個(gè)行業(yè)、數(shù)千家企業(yè),支持?jǐn)?shù)百萬(wàn)設(shè)備及應(yīng)用。
本文中的CalendarWidget示例展示了QCalendarWidget的用法。在上文中(點(diǎn)擊這里回顧>>)我們主要介紹了創(chuàng)建日歷的關(guān)鍵部件QCalendarWidget的屬性、Window類定義等,本節(jié)就繼續(xù)介紹Window類的實(shí)現(xiàn),請(qǐng)繼續(xù)關(guān)注我們喲~
QCalendarWidget一次顯示一個(gè)日歷月,并允許用戶選擇一個(gè)日期。日歷由四個(gè)組件組成:一個(gè)允許用戶更改顯示月份的導(dǎo)航欄、一個(gè)網(wǎng)格、其中每個(gè)單元格表示一個(gè)月中的一天,以及兩個(gè)顯示星期名稱和星期數(shù)字的標(biāo)題。
Qt技術(shù)交流群:166830288 歡迎一起進(jìn)群討論
我們從組合框上的周開始設(shè)置開始,此組合框控制哪一天應(yīng)顯示為一周的第一天。
QComboBox類允許我們將用戶數(shù)據(jù)作為QVariant附加到每個(gè)項(xiàng)目,稍后可以使用QComboBox的itemData()函數(shù)檢索數(shù)據(jù)。QVariant不直接支持Qt::DayOfWeek數(shù)據(jù)類型,但它支持int,c++會(huì)很樂意將任何enum值轉(zhuǎn)換為int。
... connect(localeCombo, &QComboBox::currentIndexChanged, this, &Window::localeChanged); connect(firstDayCombo, &QComboBox::currentIndexChanged, this, &Window::firstDayChanged); connect(selectionModeCombo, &QComboBox::currentIndexChanged, this, &Window::selectionModeChanged); connect(gridCheckBox, &QCheckBox::toggled, calendar, &QCalendarWidget::setGridVisible); connect(navigationCheckBox, &QCheckBox::toggled, calendar, &QCalendarWidget::setNavigationBarVisible); connect(horizontalHeaderCombo, &QComboBox::currentIndexChanged, this, &Window::horizontalHeaderChanged); connect(verticalHeaderCombo, &QComboBox::currentIndexChanged, this, &Window::verticalHeaderChanged); ...
在創(chuàng)建了小部件之后,我們連接信號(hào)和插槽,將組合框連接到Window的私有槽或QComboBox提供的公共槽。
... firstDayChanged(firstDayCombo->currentIndex()); selectionModeChanged(selectionModeCombo->currentIndex()); horizontalHeaderChanged(horizontalHeaderCombo->currentIndex()); verticalHeaderChanged(verticalHeaderCombo->currentIndex()); }
在函數(shù)的最后,我們調(diào)用更新日歷的槽,以確保QCalendarWidget在啟動(dòng)時(shí)與其他小部件同步。
現(xiàn)在讓我們看一下createDatesGroupBox()私有函數(shù):
void Window::createDatesGroupBox() { datesGroupBox = new QGroupBox(tr("Dates")); minimumDateEdit = new QDateEdit; minimumDateEdit->setDisplayFormat("MMM d yyyy"); minimumDateEdit->setDateRange(calendar->minimumDate(), calendar->maximumDate()); minimumDateEdit->setDate(calendar->minimumDate()); minimumDateLabel = new QLabel(tr("&Minimum Date:")); minimumDateLabel->setBuddy(minimumDateEdit); currentDateEdit = new QDateEdit; currentDateEdit->setDisplayFormat("MMM d yyyy"); currentDateEdit->setDate(calendar->selectedDate()); currentDateEdit->setDateRange(calendar->minimumDate(), calendar->maximumDate()); currentDateLabel = new QLabel(tr("&Current Date:")); currentDateLabel->setBuddy(currentDateEdit); maximumDateEdit = new QDateEdit; maximumDateEdit->setDisplayFormat("MMM d yyyy"); maximumDateEdit->setDateRange(calendar->minimumDate(), calendar->maximumDate()); maximumDateEdit->setDate(calendar->maximumDate()); maximumDateLabel = new QLabel(tr("Ma&ximum Date:")); maximumDateLabel->setBuddy(maximumDateEdit);
在這個(gè)函數(shù)中,我們創(chuàng)建最小日期、最大日期和當(dāng)前日期編輯器小部件,它們控制日歷的最小日期、最大日期和所選日期。日歷的最小和最大日期已經(jīng)在createPrivewGroupBox()中設(shè)置;然后可以將小部件的默認(rèn)值設(shè)置為日歷值。
connect(currentDateEdit, &QDateEdit::dateChanged, calendar, &QCalendarWidget::setSelectedDate); connect(calendar, &QCalendarWidget::selectionChanged, this, &Window::selectedDateChanged); connect(minimumDateEdit, &QDateEdit::dateChanged, this, &Window::minimumDateChanged); connect(maximumDateEdit, &QDateEdit::dateChanged, this, &Window::maximumDateChanged); ... }
我們將currentDateEdit的dateChanged()信號(hào)直接連接到日歷的setSelectedDate()插槽,當(dāng)日歷所選日期發(fā)生更改時(shí),無(wú)論是由于用戶操作還是通過編程方式,selectedDateChanged()槽都會(huì)更新Current date編輯器。我們還需要在用戶更改最小日期和最大日期編輯器時(shí)做出反應(yīng)。
下面是createTextFormatsGroup()函數(shù):
void Window::createTextFormatsGroupBox() { textFormatsGroupBox = new QGroupBox(tr("Text Formats")); weekdayColorCombo = createColorComboBox(); weekdayColorCombo->setCurrentIndex( weekdayColorCombo->findText(tr("Black"))); weekdayColorLabel = new QLabel(tr("&Weekday color:")); weekdayColorLabel->setBuddy(weekdayColorCombo); weekendColorCombo = createColorComboBox(); weekendColorCombo->setCurrentIndex( weekendColorCombo->findText(tr("Red"))); weekendColorLabel = new QLabel(tr("Week&end color:")); weekendColorLabel->setBuddy(weekendColorCombo);
我們使用createColorCombo()來(lái)設(shè)置工作日顏色和周末顏色組合框,它實(shí)例化了一個(gè)QComboBox,并用顏色(“紅色”、“藍(lán)色”等)填充它。
headerTextFormatCombo = new QComboBox; headerTextFormatCombo->addItem(tr("Bold")); headerTextFormatCombo->addItem(tr("Italic")); headerTextFormatCombo->addItem(tr("Plain")); headerTextFormatLabel = new QLabel(tr("&Header text:")); headerTextFormatLabel->setBuddy(headerTextFormatCombo); firstFridayCheckBox = new QCheckBox(tr("&First Friday in blue")); mayFirstCheckBox = new QCheckBox(tr("May &1 in red"));
Header Text Format組合框允許用戶更改用于水平和垂直標(biāo)題的文本格式(粗體、斜體或普通),藍(lán)色的First Friday和紅色的May 1復(fù)選框會(huì)影響特定日期的呈現(xiàn)。
connect(weekdayColorCombo, &QComboBox::currentIndexChanged, this, &Window::weekdayFormatChanged); connect(weekdayColorCombo, &QComboBox::currentIndexChanged, this, &Window::reformatCalendarPage); connect(weekendColorCombo, &QComboBox::currentIndexChanged, this, &Window::weekendFormatChanged); connect(weekendColorCombo, &QComboBox::currentIndexChanged, this, &Window::reformatCalendarPage); connect(headerTextFormatCombo, &QComboBox::currentIndexChanged, this, &Window::reformatHeaders); connect(firstFridayCheckBox, &QCheckBox::toggled, this, &Window::reformatCalendarPage); connect(mayFirstCheckBox, &QCheckBox::toggled, this, &Window::reformatCalendarPage);
篇幅有限,更多內(nèi)容持續(xù)關(guān)注,下期見~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)