void QCalendarWidget::activated ( const QDate & date ) [signal]
This signal is emitted whenever the user presses the Return or Enter key or double-clicks a date in the calendar widget.
void QCalendarWidget::clicked ( const QDate & date ) [signal]
This signal is emitted when a mouse button is clicked. The date the mouse was clicked on is specified by date. The signal is only emitted when clicked on a valid date, e.g., dates are not outside the minimumDate() and maximumDate(). If the selection mode is NoSelection, this signal will not be emitted.
通過文檔我們可以看出來,是區分單擊和雙擊的。
並且如果采用clicked信號,要注意設置sectionMode。
查了下文檔,出了NoSelection就是SingleSelection,直接設為SingleSelection。
我現在假設實現:點擊一個Button,然后彈出一個自帶的calendar空間,然后選中一個,然后返回,在QLineEdit中輸出。
構造函數如下:
ui.setupUi(this);
ui.calendarWidget->setMinimumDate(QDate(2006, 6, 19));
ui.calendarWidget->hide();
connect(ui.pushButton,SIGNAL(clicked()),ui.calendarWidget,SLOT(show()));
ui.calendarWidget->setSelectionMode(QCalendarWidget::SingleSelection);
connect(ui.calendarWidget,SIGNAL(clicked(const QDate &)),this,SLOT(setdate()));
原本的日歷控件是隱藏的,點擊后了就顯示,然后選中,由setdate()函數負責在lineEdit里顯示日期。
setdate()槽函數如下:
QDate date=ui.calendarWidget->selectedDate();
QString dtstr=date.toString("yyyy-MM-dd");
ui.lineEdit->setText(dtstr);
ui.calendarWidget->hide();