翻譯|使用教程|編輯:龔雪|2023-06-28 10:10:05.490|閱讀 179 次
概述:本文主要演示如何使用CalendarWidget組件來為Qt Widget應(yīng)用程序創(chuàng)建日歷,歡迎下載相關(guān)的Qt Widget組件體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Qt 是目前最先進(jìn)、最完整的跨平臺C++開發(fā)工具。它不僅完全實(shí)現(xiàn)了一次編寫,所有平臺無差別運(yùn)行,更提供了幾乎所有開發(fā)過程中需要用到的工具。如今,Qt已被運(yùn)用于超過70個(gè)行業(yè)、數(shù)千家企業(yè),支持數(shù)百萬設(shè)備及應(yīng)用。
本文中的CalendarWidget示例展示了QCalendarWidget的用法。在上文中(點(diǎn)擊這里回顧>>),我們?yōu)榇蠹医榻B了窗口類的定義和部分窗口類的實(shí)現(xiàn),本節(jié)將繼續(xù)為大家講解窗口類的實(shí)現(xiàn)。
Qt技術(shù)交流群:166830288 歡迎一起進(jìn)群討論
 
 
QCalendarWidget一次顯示一個(gè)日歷月,并允許用戶選擇一個(gè)日期。日歷由四個(gè)組件組成:一個(gè)允許用戶更改顯示月份的導(dǎo)航欄、一個(gè)網(wǎng)格,其中每個(gè)單元格表示一個(gè)月中的一天,以及兩個(gè)顯示星期名稱和星期數(shù)字的標(biāo)題。
Calendar Widget示例顯示一個(gè)QCalendarWidget,并允許用戶使用QComboBoxes、QCheckBoxes和QDateEdits配置其外觀和操作,此外,用戶可以影響單個(gè)日期和標(biāo)題的格式。
本示例包含一個(gè)類Window,它創(chuàng)建并布局QCalendarWidget和其他讓用戶配置QCalendarWidget的小部件。
現(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)建Minimum Date, Maximum Date和Current Date編輯器小部件,它們控制日歷的最小日期、最大日期和所選日期。日歷的最小和最大日期已經(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的()信號直接連接到日歷的()插槽,當(dāng)日歷所選日期發(fā)生更改時(shí),無論是由于用戶操作還是通過編程方式,selectedDateChanged()槽都會更新Current date編輯器,我們還需要在用戶 Minimum Date和Maximum 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()來設(shè)置工作日顏色和周末顏色組合框,它實(shí)例化了一個(gè)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組合框允許用戶更改用于水平和垂直標(biāo)題的文本格式(粗體、斜體或普通), First Friday in blue和May 1 in red復(fù)選框會影響特定日期的呈現(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);
我們將復(fù)選框和組合框連接到各種私有插槽,F(xiàn)irst Friday in blue和May 1 in red復(fù)選框連接到reformatCalendarPage(),該方法在日歷切換月份時(shí)也被調(diào)用。
... reformatHeaders(); reformatCalendarPage(); }
在createTextFormatsGroupBox()的末尾,我們調(diào)用私有槽來同步QCalendarWidget與其他小部件。
現(xiàn)在我們已經(jīng)檢查了四個(gè)create…GroupBox()函數(shù),看一下其他私有函數(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è)存儲用戶數(shù)據(jù)的QVariant(在本例中是QColor對象)。
此函數(shù)用于設(shè)置Weekday Color和Weekend Color組合框。
void Window::firstDayChanged(int index)
{
calendar->setFirstDayOfWeek(Qt::DayOfWeek(
firstDayCombo->itemData(index).toInt()));
}
當(dāng)用戶更改組合框值上的Week starts時(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,還更新了Maximum Date編輯器,因?yàn)槿绻碌淖钚∪掌谕碛诋?dāng)前的最大日期,QCalendarWidget將自動調(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對象作為與該項(xiàng)文本對應(yīng)的用戶數(shù)據(jù),從組合框中獲取顏色后,我們設(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()相同,不同之處是它影響的是周六和周日,而不是周一到周五。
當(dāng)用戶更改標(biāo)題的文本格式時(shí),將調(diào)用reformatHeaders()插槽。我們比較標(biāo)題文本格式組合框的當(dāng)前文本,以確定應(yīng)用哪種格式。(另一種選擇是將QTextCharFormat值存儲在組合框項(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()中,我們設(shè)置了該月的第一個(gè)星期五和當(dāng)年的5月1日的文本格式,實(shí)際使用的文本格式取決于選中了哪些復(fù)選框以及工作日/周末的格式。
QCalendarWidget允許我們使用setDateTextFormat()設(shè)置單個(gè)日期的文本格式,選擇在日歷頁面更改時(shí)設(shè)置日期格式-即顯示新的月份-以及工作日/周末格式更改時(shí)設(shè)置日期格式。我們檢查mayFirstCheckBox和firstDayCheckBox中的哪個(gè)被選中(如果有的話),并相應(yīng)地設(shè)置文本格式。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)