您的位置:首页 > 编程语言 > Qt开发

Qt学习十:日历组件

2016-06-30 17:52 471 查看
日历组件 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);
}

最后程序运行结果为:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: