目的:
制作简易电子时钟实时获取显示Windows时间
最终结果如图
下面是源代码:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void showtime1();
void on_pushButton_clicked(bool checked);
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include<QTime>
#include<QTimer>
#include<QDateTime>
#include<QLCDNumber>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QTimer *timer=new QTimer(this);
timer->start(1000);
connect(timer,SIGNAL(timeout()),this,SLOT(showtime1()));
//connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(showtime2()));
ui->pushButton->setCheckable(true);//按钮每次按下的值不同
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showtime1()
{
QTime tm=QTime::currentTime();
QString str =tm.toString("hh:mm:ss");
QDate dt=QDate::currentDate();
QString str2=dt.toString("yyyy-MM-dd");
ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
ui->lcdNumber->setStyleSheet("QLCDNumber{color:red;background:yellow}");
ui->lcdNumber->setDigitCount(20);
ui->lcdNumber->display(str2+" "+str);
}
void Dialog::on_pushButton_clicked(bool checked)
{
if(checked)
ui->lcdNumber->show();
else
ui->lcdNumber->hide();
}
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
好了到这已经制作完成了。
最后如果想在debug目录下直接运行exe可执行文件,如果提示失败将提示缺失文件在Qt目录下找到文件并复制到windows下的C:\Windows\SysWOW64目录下即可;
第一步找到缺少文件并复制
第二部粘贴至下面路径
喜欢就点个赞吧!