日历组件 qtButton.h如下:
#ifndef QTBUTTON_H
#define QTBUTTON_H
#include <QtWidgets/QMainWindow>
#include "ui_qtbutton.h"
//---------日历组件----------------------
#include<qlabel.h>
#include<qlineedit.h>
#include<qcalendarwidget.h>
class qtButton : public QMainWindow
{
Q_OBJECT
public:
qtButton(QWidget *parent = 0);
~qtButton();
private:
Ui::qtButtonClass ui;
//-------------日历组件--------------------
QLabel *label;
QLineEdit *lineEdit;
QCalendarWidget *calendarWidget;
private slots:
//-----------日历组件---------
void showTime();
void setData();
};
#endif // QTBUTTON_H
qtButton.cpp如下
#include "qtbutton.h"
//------------------------------
qtButton::qtButton(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//-----------日历组件---------
label = new QLabel(this);
label->setText("choose data:");
label->setGeometry(QRect(50, 50, 100, 25));
lineEdit = new QLineEdit(this);
lineEdit->setGeometry(QRect(130, 50, 150, 22));
connect(lineEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(showTime()));
calendarWidget = new QCalendarWidget(this);
calendarWidget->setGeometry(20, 75, 350, 180);
calendarWidget->setHidden(true);
connect(calendarWidget, SIGNAL(clicked(QDate)), this, SLOT(setData));
}
qtButton::~qtButton()
{
}
void qtButton::showTime()
{
calendarWidget->setHidden(false);
}
void qtButton::setData()
{
QDate date = calendarWidget->selectedDate();
QString str = date.toString("yyyy-MM-dd");
lineEdit->setText(str);
calendarWidget->setHidden(true);
}
最后程序运行结果为: