qcalendarwidget.cpp

来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 2,019 行 · 第 1/4 页

CPP
2,019
字号
    By default, the minimum date is the earliest date that the QDate    class can handle.    When setting a minimum date, the maximumDate and selectedDate    properties are adjusted if the selection range becomes invalid. If    the provided date is not a valid QDate object, the    setMinimumDate() function does nothing.    \sa setDateRange()*/QDate QCalendarWidget::minimumDate() const{    Q_D(const QCalendarWidget);    return d->m_model->minimumDate;}void QCalendarWidget::setMinimumDate(const QDate &date){    Q_D(QCalendarWidget);    if (!date.isValid() || d->m_model->minimumDate == date)        return;    QDate oldDate = d->m_model->date;    d->m_model->setMinimumDate(date);    d->yearEdit->setMinimum(d->m_model->minimumDate.year());    d->updateMonthMenu();    QDate newDate = d->m_model->date;    if (oldDate != newDate) {        d->update();        d->showMonth(newDate.year(), newDate.month());        emit selectionChanged();    }}/*!    \property QCalendarWidget::maximumDate    \brief the maximum date of the currently specified date range.    The user will not be able to select a date which is after the    currently set maximum date.    \table    \row    \o \image qcalendarwidget-maximum.png    \row    \o    \code    QCalendarWidget *calendar;    calendar->setGridVisible(true);    calendar->setMaximumDate(QDate(2006, 7, 3));    \endcode    \endtable    By default, the maximum date is the last day the QDate class can    handle.    When setting a maximum date, the minimumDate and selectedDate    properties are adjusted if the selection range becomes invalid. If    the provided date is not a valid QDate object, the    setMaximumDate() function does nothing.    \sa setDateRange()*/QDate QCalendarWidget::maximumDate() const{    Q_D(const QCalendarWidget);    return d->m_model->maximumDate;}void QCalendarWidget::setMaximumDate(const QDate &date){    Q_D(QCalendarWidget);    if (!date.isValid() || d->m_model->maximumDate == date)        return;    QDate oldDate = d->m_model->date;    d->m_model->setMaximumDate(date);    d->yearEdit->setMaximum(d->m_model->maximumDate.year());    d->updateMonthMenu();    QDate newDate = d->m_model->date;    if (oldDate != newDate) {        d->update();        d->showMonth(newDate.year(), newDate.month());        emit selectionChanged();    }}/*!    Defines a date range by setting the minimumDate and maximumDate    properties.    The date range restricts the user selection, i.e. the user can    only select dates within the specified date range. Note that    \code        QCalendarWidget *calendar;        calendar->setDateRange(min, max);    \endcode    is analogous to    \code        QCalendarWidget *calendar;        calendar->setMinimumDate(min);        calendar->setMaximumDate(max);    \endcode    If either the \a min or \a max parameters are not valid QDate    objects, this function does nothing.    \sa setMinimumDate(), setMaximumDate()*/void QCalendarWidget::setDateRange(const QDate &min, const QDate &max){    Q_D(QCalendarWidget);    if (d->m_model->minimumDate == min && d->m_model->maximumDate == max)        return;    if (!min.isValid() || !max.isValid())        return;    QDate minimum = min;    QDate maximum = max;    if (min > max) {        minimum = max;        maximum = min;    }    QDate oldDate = d->m_model->date;    d->m_model->setRange(min, max);    d->yearEdit->setMinimum(d->m_model->minimumDate.year());    d->yearEdit->setMaximum(d->m_model->maximumDate.year());    d->updateMonthMenu();    QDate newDate = d->m_model->date;    if (oldDate != newDate) {        d->update();        d->showMonth(newDate.year(), newDate.month());        emit selectionChanged();    }}/*! \enum QCalendarWidget::HorizontalHeaderFormat    This enum type defines the various formats the horizontal header can display.    \value SingleLetterDayNames The header displays a single letter abbreviation for day names (e.g. M for Monday).    \value ShortDayNames The header displays a short abbreviation for day names (e.g. Mon for Monday).    \value LongDayNames The header displays complete day names (e.g. Monday).    \value NoHorizontalHeader The header is hidden.    \sa horizontalHeaderFormat(), VerticalHeaderFormat*//*!    \property QCalendarWidget::horizontalHeaderFormat    \brief the format of the horizontal header.    The default value is QCalendarWidget::ShortDayNames.*/void QCalendarWidget::setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format){    Q_D(QCalendarWidget);    if (d->m_model->horizontalHeaderFormat == format)        return;    d->m_model->setHorizontalHeaderFormat(format);    d->m_view->viewport()->update();    d->m_view->updateGeometry();}QCalendarWidget::HorizontalHeaderFormat QCalendarWidget::horizontalHeaderFormat() const{    Q_D(const QCalendarWidget);    return d->m_model->horizontalHeaderFormat;}/*! \enum QCalendarWidget::VerticalHeaderFormat    This enum type defines the various formats the vertical header can display.    \value ISOWeekNumbers The header displays a ISO week numbers \l QDate::weekNumber().    \value NoVerticalHeader The header is hidden.    \sa verticalHeaderFormat(), HorizontalHeaderFormat*//*!    \property QCalendarWidget::verticalHeaderFormat    \brief the format of the vertical header.    The default value is QCalendarWidget::ISOWeekNumber.*/QCalendarWidget::VerticalHeaderFormat QCalendarWidget::verticalHeaderFormat() const{    Q_D(const QCalendarWidget);    bool shown = d->m_model->weekNumbersShown();    if (shown)        return QCalendarWidget::ISOWeekNumbers;    return QCalendarWidget::NoVerticalHeader;}void QCalendarWidget::setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat format){    Q_D(QCalendarWidget);    bool show = false;    if (format == QCalendarWidget::ISOWeekNumbers)        show = true;    if (d->m_model->weekNumbersShown() == show)        return;    d->m_model->setWeekNumbersShown(show);    d->m_view->viewport()->update();    d->m_view->updateGeometry();}/*!    \property QCalendarWidget::gridVisible    \brief whether the table grid is displayed.    \table    \row        \o \inlineimage qcalendarwidget-grid.png    \row        \o        \code            QCalendarWidget *calendar;            calendar->setGridVisible(true);        \endcode    \endtable    The default value is false.*/bool QCalendarWidget::isGridVisible() const{    Q_D(const QCalendarWidget);    return d->m_view->showGrid();}void QCalendarWidget::setGridVisible(bool show){    Q_D(QCalendarWidget);    d->m_view->setShowGrid(show);    d->m_view->viewport()->update();    d->m_view->updateGeometry();}/*!    \property QCalendarWidget::selectionMode    \brief the type of selection the user can make in the calendar    When this property is set to SingleSelection, the user can select a date    within the minimum and maximum allowed dates, using either the mouse or    the keyboard.    When the property is set to NoSelection, the user will be unable to select    dates, but they can still be selected programmatically. Note that the date     that is selected when the property is set to NoSelection will still be     the selected date of the calendar.    The default value is SingleSelection.*/QCalendarWidget::SelectionMode QCalendarWidget::selectionMode() const{    Q_D(const QCalendarWidget);    return d->m_view->readOnly ? QCalendarWidget::NoSelection : QCalendarWidget::SingleSelection;}void QCalendarWidget::setSelectionMode(SelectionMode mode){    Q_D(QCalendarWidget);    d->m_view->readOnly = (mode == QCalendarWidget::NoSelection);}/*!    \property QCalendarWidget::firstDayOfWeek    \brief a value identifying the day displayed in the first column.    By default, the day displayed in the first column is Sunday*/void QCalendarWidget::setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek){    Q_D(QCalendarWidget);    if ((Qt::DayOfWeek)d->m_model->firstColumnDay() == dayOfWeek)        return;    d->m_model->setFirstColumnDay(dayOfWeek);    d->update();}Qt::DayOfWeek QCalendarWidget::firstDayOfWeek() const{    Q_D(const QCalendarWidget);    return (Qt::DayOfWeek)d->m_model->firstColumnDay();}/*!    Returns the text char format for rendering the header.*/QTextCharFormat QCalendarWidget::headerTextFormat() const{    Q_D(const QCalendarWidget);    return d->m_model->m_headerFormat;}/*!    Sets the text char format for rendering the header to \a format.    If you also set a weekday text format, this format's foreground and    background color will take precedence over the header's format.     The other formatting information will still be decided by     the header's format. */void QCalendarWidget::setHeaderTextFormat(const QTextCharFormat &format){    Q_D(QCalendarWidget);    d->m_model->m_headerFormat = format;    d->m_view->viewport()->update();    d->m_view->updateGeometry();}/*!    Returns the text char format for rendering of day in the week \a dayOfWeek.    \sa headerTextFormat()*/QTextCharFormat QCalendarWidget::weekdayTextFormat(Qt::DayOfWeek dayOfWeek) const{    Q_D(const QCalendarWidget);    return d->m_model->m_dayFormats.value(dayOfWeek);}/*!    Sets the text char format for rendering of day in the week \a dayOfWeek to \a format.    The format will take precedence over the header format in case of foreground     and background color. Other text formatting information is taken from the headers format.     \sa setHeaderTextFormat()*/void QCalendarWidget::setWeekdayTextFormat(Qt::DayOfWeek dayOfWeek, const QTextCharFormat &format){    Q_D(QCalendarWidget);    d->m_model->m_dayFormats[dayOfWeek] = format;    d->m_view->viewport()->update();    d->m_view->updateGeometry();}/*!    Returns a QMap from QDate to QTextCharFormat showing all dates    that use a special format that alters their rendering.*/QMap<QDate, QTextCharFormat> QCalendarWidget::dateTextFormat() const{    Q_D(const QCalendarWidget);    return d->m_model->m_dateFormats;}/*!    Returns a QTextCharFormat for \a date. The char format can be be    empty if the date is not renderd specially.*/QTextCharFormat QCalendarWidget::dateTextFormat(const QDate &date) const{    Q_D(const QCalendarWidget);    return d->m_model->m_dateFormats.value(date);}/*!    Sets \a format to render \a date.*/void QCalendarWidget::setDateTextFormat(const QDate &date, const QTextCharFormat &format){    Q_D(QCalendarWidget);    d->m_model->m_dateFormats[date] = format;    d->m_view->viewport()->update();    d->m_view->updateGeometry();}/*!    \fn void QCalendarWidget::selectionChanged()    This signal is emitted when the currently selected date is    changed.    The currently selected date can be changed by the user using the    mouse or keyboard, or by the programmer using setSelectedDate().    \sa selectedDate()*//*!    \fn void QCalendarWidget::currentPageChanged(int year, int month)    This signal is emitted when the currently shown month is changed.    The new \a year and \a month are passed as parameters.    \sa setCurrentPage()*//*!    \fn void QCalendarWidget::activated(const QDate &date)    This signal is emitted whenever the user presses the Return or    Enter key, the space bar or double-clicks a \a date in the calendar    widget.*//*!    \fn void QCalendarWidget::clicked(const QDate &date)    This signal is emitted when a mouse button is clicked. The date the    mouse was clicked on is specified by \a date. The signal is only    emitted when clicked on a valid date.*//*!    \property QCalendarWidget::headerVisible    \brief whether the Header is shown or not    When this property is set to true the next month, previous month,    month selection, year selection controls are shown on top    When the property is set to false, these controls are hidden.    The default value is true.*/bool QCalendarWidget::isHeaderVisible() const{    Q_D(const QCalendarWidget);    return d->headerVisible;}void QCalendarWidget::setHeaderVisible(bool show){    Q_D(QCalendarWidget);    d->headerVisible = show;    (show)?d->headerBackground->show():d->headerBackground->hide();    updateGeometry();}/*!  \reimp*/bool QCalendarWidget::event(QEvent *event){    Q_D(QCalendarWidget);    if (event->type() == QEvent::FontChange || event->type() == QEvent::ApplicationFontChange) {        d->updateHeader();        d->m_view->updateGeometry();    }    return QWidget::event(event);}/*!  \reimp*/void QCalendarWidget::mousePressEvent(QMouseEvent *event){    setAttribute(Qt::WA_NoMouseReplay);    QWidget::mousePressEvent(event);    setFocus();}/*!  \reimp*/void QCalendarWidget::resizeEvent(QResizeEvent * event){    Q_D(QCalendarWidget);    if(d->yearEdit->isVisible())        d->_q_yearEditingFinished();    QWidget::resizeEvent(event);}/*!  \reimp*/void QCalendarWidget::keyPressEvent(QKeyEvent * event){    Q_D(QCalendarWidget);    if(d->yearEdit->isVisible()&& event->key() == Qt::Key_Escape)    {        d->yearEdit->setValue(yearShown());        d->_q_yearEditingFinished();        return;    }    QWidget::keyReleaseEvent(event);}#include "qcalendarwidget.moc"#include "moc_qcalendarwidget.cpp"#endif //QT_NO_CALENDARWIDGET

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?