Disable choosing date in the future in QCalendarWidget

时间:2016-08-23 15:35:53

标签: c++ qt

This is the standard calendar

calendar

I want to make the dates from tomorrow in this calendar appear in grey, and we can not select the date from tomorrow on, we can only select date from the past until today. Is that possible?

This is my code:

MyClass::MyClass(QcgDatabase * db, int hostid, QWidget *parent)
: QWidget(parent)
{
    ui.setupUi(this);
    connect(ui.tableWidget, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(calendar_clicked(QTableWidgetItem*)));
}

void MyClass::calendar_clicked(QTableWidgetItem* tableitem) 
{
    int column = tableitem->column();
    clicked_item = tableitem;

    if (!calendar) {
        calendar = new QCalendarWidget();
    }
    calendar->setWindowTitle("Calendar");
    calendar->setWindowModality(Qt::WindowModal);
    calendar->show();
    connect(calendar, SIGNAL(activated(const QDate&)), this, SLOT(date_selected(const QDate&)));
}

void MyClass::date_selected(const QDate&)
{
    if (!clicked_item) {
        return;
    }
    QIcon icon(":/myclass/icon/calendar.png");
    clicked_item->setIcon(icon);
    clicked_item->setText(calendar->selectedDate().toString("dd.MM.yyyy"));
    calendar->close();
}

1 个答案:

答案 0 :(得分:3)

calendar->setMaximumDate(QDate::currentDate());