gnase gnase - 3 months ago 21
C++ Question

Disable choosing date in the future in QCalendarWidget

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

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