/*基于qt5.7
*无需ui界面
*资料是百度和自几学到的,不多
*仅供参考
*不喜随便喷,反正我也不看,哈啊啊、
*/
//首先是dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QLCDNumber>
#include <QTime>
#include <QTimer>
#include <QPalette>
#include <QVBoxLayout>
#include <QHBoxLayout>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void showTime();
private:
Ui::Dialog *ui;
QLCDNumber *time;
QTime tim;
QTimer *timer;
};
#endif // DIALOG_H
//接着是dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
resize(200,200); //设置生成界面的大小,可以拖边框改变界面大小
time = new QLCDNumber(this);
time->setDigitCount(8); //设置lcd里面的个数,格式是hh:mm:ss,总的是八个。所以设置为8
time->setFixedSize(80, 60); //设置大小
time->setPalette(Qt::cyan); //设置颜色
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
vbox->addWidget(time);
hbox->addLayout(vbox);
setLayout(hbox); //设置布局,将lcd空间放到中间
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime())); //用一个定时信号来更改时间
timer->start(500); //启动定时
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showTime()
{
tim = QTime::currentTime(); //获取当地时间
QString timestr = tim.toString("hh:mm:ss"); //设置时间格式
time->display(timestr); //显示时间
}
//mian.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}