QT实现日历显示

时间:2022-12-12 11:20:03
void Window::localeChanged(int index)
{
const QLocale newLocale(localeCombo->itemData(index).toLocale());
calendar->setLocale(newLocale);
int newLocaleFirstDayIndex = firstDayCombo->findData(newLocale.firstDayOfWeek());
firstDayCombo->setCurrentIndex(newLocaleFirstDayIndex);
}


void Window::firstDayChanged(int index)
{
calendar->setFirstDayOfWeek(Qt::DayOfWeek(
firstDayCombo->itemData(index).toInt()));
}


void Window::selectionModeChanged(int index)
{
calendar->setSelectionMode(QCalendarWidget::SelectionMode(
selectionModeCombo->itemData(index).toInt()));
}

void Window::horizontalHeaderChanged(int index)
{
calendar->setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat(
horizontalHeaderCombo->itemData(index).toInt()));
}

void Window::verticalHeaderChanged(int index)
{
calendar->setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat(
verticalHeaderCombo->itemData(index).toInt()));
}


void Window::selectedDateChanged()
{
currentDateEdit->setDate(calendar->selectedDate());
}



void Window::minimumDateChanged(const QDate &date)
{
calendar->setMinimumDate(date);
maximumDateEdit->setDate(calendar->maximumDate());
}



void Window::maximumDateChanged(const QDate &date)
{
calendar->setMaximumDate(date);
minimumDateEdit->setDate(calendar->minimumDate());
}


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);
}



void Window::weekendFormatChanged()
{
QTextCharFormat format;

format.setForeground(qvariant_cast<QColor>(
weekendColorCombo->itemData(weekendColorCombo->currentIndex())));
calendar->setWeekdayTextFormat(Qt::Saturday, format);
calendar->setWeekdayTextFormat(Qt::Sunday, format);
}



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);
}

QT实现日历显示