QCalendarWidget的使用

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

posted on 2013-07-02 10:26  Q11三工鸟yue  阅读(4172)  评论(0编辑  收藏  举报