Qt学习十:日历组件

日历组件 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);
}

最后程序运行结果为:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值