翻譯|使用教程|編輯:龔雪|2024-01-10 10:24:28.230|閱讀 198 次
概述:本文將為大家介紹如何使用Qt Widget小部件中的QCalendarWidget來實現日歷功能,歡迎下載最新版組件體驗~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Qt 是目前最先進、最完整的跨平臺C++開發工具。它不僅完全實現了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發過程中需要用到的工具。如今,Qt已被運用于超過70個行業、數千家企業,支持數百萬設備及應用。
本文中的CalendarWidget示例展示了QCalendarWidget的用法。在上文中(點擊這里回顧>>)我們主要介紹了創建日歷的關鍵部件QCalendarWidget的屬性、Window類定義等,本節就繼續介紹Window類的實現,請繼續關注我們喲~
QCalendarWidget一次顯示一個日歷月,并允許用戶選擇一個日期。日歷由四個組件組成:一個允許用戶更改顯示月份的導航欄、一個網格、其中每個單元格表示一個月中的一天,以及兩個顯示星期名稱和星期數字的標題。
Qt技術交流群:166830288 歡迎一起進群討論
我們從組合框上的周開始設置開始,此組合框控制哪一天應顯示為一周的第一天。
QComboBox類允許我們將用戶數據作為QVariant附加到每個項目,稍后可以使用QComboBox的itemData()函數檢索數據。QVariant不直接支持Qt::DayOfWeek數據類型,但它支持int,c++會很樂意將任何enum值轉換為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); ...
在創建了小部件之后,我們連接信號和插槽,將組合框連接到Window的私有槽或QComboBox提供的公共槽。
... firstDayChanged(firstDayCombo->currentIndex()); selectionModeChanged(selectionModeCombo->currentIndex()); horizontalHeaderChanged(horizontalHeaderCombo->currentIndex()); verticalHeaderChanged(verticalHeaderCombo->currentIndex()); }
在函數的最后,我們調用更新日歷的槽,以確保QCalendarWidget在啟動時與其他小部件同步。
現在讓我們看一下createDatesGroupBox()私有函數:
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);
在這個函數中,我們創建最小日期、最大日期和當前日期編輯器小部件,它們控制日歷的最小日期、最大日期和所選日期。日歷的最小和最大日期已經在createPrivewGroupBox()中設置;然后可以將小部件的默認值設置為日歷值。
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()信號直接連接到日歷的setSelectedDate()插槽,當日歷所選日期發生更改時,無論是由于用戶操作還是通過編程方式,selectedDateChanged()槽都會更新Current date編輯器。我們還需要在用戶更改最小日期和最大日期編輯器時做出反應。
下面是createTextFormatsGroup()函數:
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()來設置工作日顏色和周末顏色組合框,它實例化了一個QComboBox,并用顏色(“紅色”、“藍色”等)填充它。
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組合框允許用戶更改用于水平和垂直標題的文本格式(粗體、斜體或普通),藍色的First Friday和紅色的May 1復選框會影響特定日期的呈現。
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);
篇幅有限,更多內容持續關注,下期見~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網