要求:点击按钮,获取系统时间并显示
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();
}