翻譯|使用教程|編輯:龔雪|2023-06-28 10:10:05.490|閱讀 165 次
概述:本文主要演示如何使用CalendarWidget組件來為Qt Widget應用程序創建日歷,歡迎下載相關的Qt Widget組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Qt 是目前最先進、最完整的跨平臺C++開發工具。它不僅完全實現了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發過程中需要用到的工具。如今,Qt已被運用于超過70個行業、數千家企業,支持數百萬設備及應用。
本文中的CalendarWidget示例展示了QCalendarWidget的用法。在上文中(點擊這里回顧>>),我們為大家介紹了窗口類的定義和部分窗口類的實現,本節將繼續為大家講解窗口類的實現。
Qt技術交流群:166830288 歡迎一起進群討論
QCalendarWidget一次顯示一個日歷月,并允許用戶選擇一個日期。日歷由四個組件組成:一個允許用戶更改顯示月份的導航欄、一個網格,其中每個單元格表示一個月中的一天,以及兩個顯示星期名稱和星期數字的標題。
Calendar Widget示例顯示一個QCalendarWidget,并允許用戶使用QComboBoxes、QCheckBoxes和QDateEdits配置其外觀和操作,此外,用戶可以影響單個日期和標題的格式。
本示例包含一個類Window,它創建并布局QCalendarWidget和其他讓用戶配置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);
在這個函數中,我們創建Minimum Date, Maximum Date和Current Date編輯器小部件,它們控制日歷的最小日期、最大日期和所選日期。日歷的最小和最大日期已經在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的()信號直接連接到日歷的()插槽,當日歷所選日期發生更改時,無論是由于用戶操作還是通過編程方式,selectedDateChanged()槽都會更新Current date編輯器,我們還需要在用戶 Minimum Date和Maximum 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,并用顏色("Red", "Blue"等)填充它。
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 in blue和May 1 in red復選框會影響特定日期的呈現。
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);
我們將復選框和組合框連接到各種私有插槽,First Friday in blue和May 1 in red復選框連接到reformatCalendarPage(),該方法在日歷切換月份時也被調用。
... reformatHeaders(); reformatCalendarPage(); }
在createTextFormatsGroupBox()的末尾,我們調用私有槽來同步QCalendarWidget與其他小部件。
現在我們已經檢查了四個create…GroupBox()函數,看一下其他私有函數和槽。
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()中,我們創建了一個組合框,并用標準顏色填充它,QComboBox::addItem()的第二個參數是一個存儲用戶數據的QVariant(在本例中是QColor對象)。
此函數用于設置Weekday Color和Weekend Color組合框。
void Window::firstDayChanged(int index) { calendar->setFirstDayOfWeek(Qt::DayOfWeek( firstDayCombo->itemData(index).toInt())); }
當用戶更改組合框值上的Week starts時,firstDayChanged()將使用組合框新值的索引調用。我們使用itemData()檢索與新的當前項關聯的自定義數據項,并將其轉換為Qt::DayOfWeek。
selectionModeChanged()、horizontalHeaderChanged()和verticalHeaderChanged()與firstDayChanged()非常相似,因此省略它們。
void Window::selectedDateChanged() { currentDateEdit->setDate(calendar->selectedDate()); } selectedDateChanged()更新Current Date編輯器,以反映QCalendarWidget的當前狀態。 void Window::minimumDateChanged(QDate date) { calendar->setMinimumDate(date); maximumDateEdit->setDate(calendar->maximumDate()); }
當用戶更改最小日期時告訴QCalenderWidget,還更新了Maximum Date編輯器,因為如果新的最小日期晚于當前的最大日期,QCalendarWidget將自動調整其最大日期以避免矛盾狀態。
void Window::maximumDateChanged(QDate date) { calendar->setMaximumDate(date); minimumDateEdit->setDate(calendar->minimumDate()); }
maximumDateChanged()的實現類似于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); }
每個組合框項都有一個QColor對象作為與該項文本對應的用戶數據,從組合框中獲取顏色后,我們設置一周中每一天的文本格式。
日歷中列的文本格式為QTextCharFormat,除了前景色外,它還允許我們指定各種字符格式信息。在這個例子中,我們只展示了可能性的一個子集。
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()相同,不同之處是它影響的是周六和周日,而不是周一到周五。
當用戶更改標題的文本格式時,將調用reformatHeaders()插槽。我們比較標題文本格式組合框的當前文本,以確定應用哪種格式。(另一種選擇是將QTextCharFormat值存儲在組合框項旁邊。)
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()中,我們設置了該月的第一個星期五和當年的5月1日的文本格式,實際使用的文本格式取決于選中了哪些復選框以及工作日/周末的格式。
QCalendarWidget允許我們使用setDateTextFormat()設置單個日期的文本格式,選擇在日歷頁面更改時設置日期格式-即顯示新的月份-以及工作日/周末格式更改時設置日期格式。我們檢查mayFirstCheckBox和firstDayCheckBox中的哪個被選中(如果有的話),并相應地設置文本格式。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網