Qt制作简易电子时钟

目的:
制作简易电子时钟实时获取显示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目录下即可;
第一步找到缺少文件并复制
第一步找到缺少文件并复制
第二部粘贴至下面路径
在这里插入图片描述
喜欢就点个赞吧!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值