翻譯|使用教程|編輯:龔雪|2024-01-23 10:40:30.700|閱讀 132 次
概述:本文將為大家介紹如何使用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ā)過(guò)程中需要用到的工具。如今,Qt已被運(yùn)用于超過(guò)70個(gè)行業(yè)、數(shù)千家企業(yè),支持?jǐn)?shù)百萬(wàn)設(shè)備及應(yīng)用。
本文中的CalendarWidget示例展示了QCalendarWidget的用法。在上文中(點(diǎn)擊這里回顧>>)我們主要介紹了Window類定義的實(shí)現(xiàn),本節(jié)就繼續(xù)介紹,請(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)群討論
接上文,我們將復(fù)選框和組合框連接到各種私有插槽,藍(lán)色復(fù)選框中的第一個(gè)星期五和紅色復(fù)選框中的May 1都連接到reformatCalendarPage(),該方法在日歷切換月份時(shí)也被調(diào)用。
... reformatHeaders(); reformatCalendarPage(); }
在createTextFormatsGroupBox()的末尾,我們調(diào)用私有槽來(lái)同步QCalendarWidget與其他小部件。
現(xiàn)在我們已經(jīng)檢查了四個(gè)create…GroupBox()函數(shù),現(xiàn)在看一下其他私有函數(shù)和槽。
QComboBox *Window::createColorComboBox() { QComboBox *comboBox = new QComboBox; comboBox->addItem(tr("Red"), QColor(Qt::red)); comboBox->addItem(tr("Blue"), QColor(Qt::blue)); comboBox->addItem(tr("Black"), QColor(Qt::black)); comboBox->addItem(tr("Magenta"), QColor(Qt::magenta)); return comboBox; }
在createColorCombo()中,我們創(chuàng)建了一個(gè)組合框,并用標(biāo)準(zhǔn)顏色填充它。QComboBox::addItem()的第二個(gè)參數(shù)是一個(gè)存儲(chǔ)用戶數(shù)據(jù)的QVariant(在本例中是QColor對(duì)象)。
此函數(shù)用于設(shè)置工作日顏色和周末顏色組合框。
void Window::firstDayChanged(int index) { calendar->setFirstDayOfWeek(Qt::DayOfWeek( firstDayCombo->itemData(index).toInt())); }
當(dāng)用戶更改組合框的值上的星期開始時(shí),firstDayChanged()將使用組合框新值的索引調(diào)用。我們使用itemData()檢索與新的當(dāng)前項(xiàng)關(guān)聯(lián)的自定義數(shù)據(jù)項(xiàng),并將其轉(zhuǎn)換為Qt::DayOfWeek。
selectionModeChanged()、horizontalHeaderChanged()和verticalHeaderChanged()與firstDayChanged()非常相似,因此省略它們。
void Window::selectedDateChanged() { currentDateEdit->setDate(calendar->selectedDate()); }
selectedDateChanged()更新Current Date編輯器,以反映QCalendarWidget的當(dāng)前狀態(tài)。
void Window::minimumDateChanged(QDate date) { calendar->setMinimumDate(date); maximumDateEdit->setDate(calendar->maximumDate()); }
當(dāng)用戶更改最小日期時(shí),我們告訴QCalenderWidget。與此同時(shí)還更新了Maximum Date編輯器,因?yàn)槿绻碌淖钚∪掌谕碛诋?dāng)前的最大日期,QCalendarWidget將自動(dòng)調(diào)整其最大日期以避免矛盾狀態(tài)。
void Window::maximumDateChanged(QDate date) { calendar->setMaximumDate(date); minimumDateEdit->setDate(calendar->minimumDate()); }
maximumDateChanged()的實(shí)現(xiàn)類似于minimumDateChanged()。
void Window::weekdayFormatChanged() { QTextCharFormat format; format.setForeground(qvariant_cast<QColor>( weekdayColorCombo->itemData(weekdayColorCombo->currentIndex()))); calendar->setWeekdayTextFormat(Qt::Monday, format); calendar->setWeekdayTextFormat(Qt::Tuesday, format); calendar->setWeekdayTextFormat(Qt::Wednesday, format); calendar->setWeekdayTextFormat(Qt::Thursday, format); calendar->setWeekdayTextFormat(Qt::Friday, format); }
每個(gè)組合框項(xiàng)都有一個(gè)QColor對(duì)象作為與該項(xiàng)文本對(duì)應(yīng)的用戶數(shù)據(jù),從組合框中獲取顏色后,我們?cè)O(shè)置一周中每一天的文本格式。
日歷中列的文本格式為QTextCharFormat,除了前景色外,它還允許我們指定各種字符格式信息。在這個(gè)例子中,我們只展示了可能性的一個(gè)子集。
void Window::weekendFormatChanged() { QTextCharFormat format; format.setForeground(qvariant_cast<QColor>( weekendColorCombo->itemData(weekendColorCombo->currentIndex()))); calendar->setWeekdayTextFormat(Qt::Saturday, format); calendar->setWeekdayTextFormat(Qt::Sunday, format); }
weekendFormatChanged()與weekdayFormatChanged()相同,不同之處是它影響的是周六和周日,而不是周一到周五。
void Window::reformatHeaders() { QString text = headerTextFormatCombo->currentText(); QTextCharFormat format; if (text == tr("Bold")) format.setFontWeight(QFont::Bold); else if (text == tr("Italic")) format.setFontItalic(true); else if (text == tr("Green")) format.setForeground(Qt::green); calendar->setHeaderTextFormat(format); }
當(dāng)用戶更改標(biāo)題的文本格式時(shí),將調(diào)用reformatHeaders()插槽。我們比較標(biāo)題文本格式組合框的當(dāng)前文本,以確定應(yīng)用哪種格式。(另一種選擇是將QTextCharFormat值存儲(chǔ)在組合框項(xiàng)旁邊。)
void Window::reformatCalendarPage() { QTextCharFormat mayFirstFormat; const QDate mayFirst(calendar->yearShown(), 5, 1); QTextCharFormat firstFridayFormat; QDate firstFriday(calendar->yearShown(), calendar->monthShown(), 1); while (firstFriday.dayOfWeek() != Qt::Friday) firstFriday = firstFriday.addDays(1); if (firstFridayCheckBox->isChecked()) { firstFridayFormat.setForeground(Qt::blue); } else { // Revert to regular colour for this day of the week. Qt::DayOfWeek dayOfWeek(static_cast<Qt::DayOfWeek>(firstFriday.dayOfWeek())); firstFridayFormat.setForeground(calendar->weekdayTextFormat(dayOfWeek).foreground()); } calendar->setDateTextFormat(firstFriday, firstFridayFormat); // When it is checked, "May First in Red" always takes precedence over "First Friday in Blue". if (mayFirstCheckBox->isChecked()) { mayFirstFormat.setForeground(Qt::red); } else if (!firstFridayCheckBox->isChecked() || firstFriday != mayFirst) { // We can now be certain we won't be resetting "May First in Red" when we restore // may 1st's regular colour for this day of the week. Qt::DayOfWeek dayOfWeek(static_cast<Qt::DayOfWeek>(mayFirst.dayOfWeek())); calendar->setDateTextFormat(mayFirst, calendar->weekdayTextFormat(dayOfWeek)); } calendar->setDateTextFormat(mayFirst, mayFirstFormat); }
在reformatCalendarPage()中,我們?cè)O(shè)置了該月的第一個(gè)星期五和當(dāng)年的5月1日的文本格式,實(shí)際使用的文本格式取決于選中了哪些復(fù)選框以及工作日/周末的格式。
QCalendarWidget允許我們使用setDateTextFormat()設(shè)置單個(gè)日期的文本格式,我們選擇在日歷頁(yè)面更改時(shí)設(shè)置日期格式-即顯示新的月份-以及工作日/周末格式更改時(shí)設(shè)置日期格式,檢查mayFirstCheckBox和firstDayCheckBox中的哪個(gè)被選中(如果有的話),并相應(yīng)地設(shè)置文本格式。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)