QT实现获取系统时间

要求:点击按钮,获取系统时间并显示

Time.h

#ifndef __TIME_H
#define __TIME_H

#include<QApplication>
#include<QLabel>
#include<QPushButton>
#include<QFont>
#include<QDialog>
#include<QVBoxLayout>
#include<QTime>
#include<QString>

class TimeDialog:public QDialog{
	Q_OBJECT
	public:
		TimeDialog(void);
	public slots:
		void showTime(void);
	private:
		QLabel* label;
		QPushButton* button;
		QVBoxLayout *layout;
		QTime time;
};


#endif

Time.cpp

#include"Time.h"

TimeDialog::TimeDialog(void){
	//设置标题
	setWindowTitle("系统时间");
	//设置字体大小
	QFont font;
	font.setPointSize(15);
	setFont(font);
	//设置标签
	label = new QLabel(this);
	label->setFrameStyle(QFrame::Panel|QFrame::Sunken);//设置标签边框并凹陷
	label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);//设置标签居中
	
	//设置按钮
	button = new QPushButton("获取时间",this);
	//设置纵向布局器
	layout = new QVBoxLayout(this);
	layout->addWidget(label);
	layout->addWidget(button);
	setLayout(layout);
//连接按钮按下后的获取系统时间并显示的槽函数
connect(button,SIGNAL(clicked(void)),this,SLOT(showTime(void)));

}
//获取系统时间并显示的槽函数
void TimeDialog::showTime(void){
	time = QTime::currentTime();
	QString str = time.toString("hh:mm:ss");
	label->setText(str);
}

main.cpp

#include"Time.h"

int main(int argc,char*argv[]){
	QApplication app(argc,argv);

	TimeDialog dialog;
	dialog.show();
	return app.exec();
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值