QT---day4事件

1、思维导图

2、 

头文件

#ifndef MYWIDGET_H
#define MYWIDGET_H
 
 
#include <QWidget>
#include<QIcon> //图标类
#include<QLabel> //标签类
#include<QMovie> //动图类
#include<QLineEdit> //行编辑器类
#include<QPushButton> //按钮类
#include <QDebug>
#include <QMessageBox>
#include<QTimer>        //定时器类
#include<QTime>            //时间类
#include<QTimerEvent>      //定时器事件处理类
#include<QDateTime>        //日期时间类
 
 
QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE
 
 
class MyWidget : public QWidget
{
    Q_OBJECT
 
 
public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
public:
    QPushButton *btn1;
    QPushButton *btn2;
     QLineEdit *edit1;
     QLineEdit *edit2;
      QLabel *lab2;
       QLabel *lab1;
 
 
private slots:
     void timeout_slot();    //自定义处理超时信号的槽函数
     void on_eventBtn_clicked();
     void timerEvent(QTimerEvent *eent) override;       //重写定时器事件处理函数
 
 
private:
    Ui::MyWidget *ui;
    QTimer timer;
    int tid =0;
};
#endif // MYWIDGET_H
 

源文件

#include "mywidget.h"
#include "ui_mywidget.h"
 
 
MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    //==============窗口相关设置=======
    this->resize(300,200);
    //this->setFixedSize(300,200);
    //窗口标题
    this->setWindowTitle("闹钟");
    //窗口图标
   // this->setWindowIcon(QIcon(":/pictrue/qq.png"));
    //背景颜色
    this->setStyleSheet("background-color:white");
    //去掉头部
    this->setWindowFlag(Qt::FramelessWindowHint);
 
 
 
 
    //============标签相关设置=======
    //QLabel *lab1 = new QLabel(this);
    //设置大小
    this->lab1->resize(100,40);
    this->lab1->move(20,10);
    this->lab1->setStyleSheet("background-color:blue");
    //lab1->setStyleSheet("background-color:pink");
    //动图类 接收动图
    //QMovie *mv = new QMovie(":/pictrue/zz.gif");
    //将动图放入标签中
    //lab1->setMovie(mv);
    //让动图动起来
    //mv->start();
    //自动适应
    //lab1->setScaledContents(true);
 
 
   // QLabel *lab2 = new QLabel(this);
    this->lab2->resize(280,100);
    this->lab2->move(10,80);
    this->lab2->setPixmap(QPixmap(":/picture/nz.png"));
    this->lab2->setScaledContents(true);
    this->lab2->setStyleSheet("background-color:blue");
 
 
 
 
    //============行编辑器相关设置=======
    this->edit1 = new QLineEdit(this);
    edit1->resize(100,30);
    edit1->move(180,10);
   // edit1->setPlaceholderText("QQ号/手机号/邮箱");
 
 
    //============按钮相关设置=======
    this->btn1 = new QPushButton("启动",this);
    btn1->resize(40,20);
    btn1->move(180,50);
    //样式函数setStyleSheet()
    btn1->setStyleSheet("background-color:rgb(8,189,253);border-radius:5px;color:white");
 
 
 
 
    //============按钮相关设置=======
    this->btn2 = new QPushButton("关闭",this);
    btn2->resize(40,20);
    btn2->move(230,50);
    //样式函数setStyleSheet()
    btn2->setStyleSheet("background-color:rgb(8,189,253);border-radius:5px;color:white");
 
 
     // connect(edit2,&QLineEdit::textChanged,this,&MyWidget::signal);
      connect(btn1,&QPushButton::setDown,this,&MyWidget::on_eventBtn_clicked);
      connect(btn2,&QPushButton::setDown,this,&MyWidget::on_eventBtn_clicked);
 
 
 
 
}
 
 
MyWidget::~MyWidget()
{
    delete ui;
}
 
 
//超时信号对应的槽函数的定义
void MyWidget::timeout_slot()
{
//    static int num = 0;
//    ui->objLab->setNum(num++);
    //使用时间类实例化对象
    QTime sysTime = QTime::currentTime();        //获取系统当前的时间,并返回一个时间类对象
 
 
 
 
    //将QTime转换为QString
    QString time = sysTime.toString("hh : mm : ss");
 
 
 
 
    //将字符串展示到ui界面
    this->edit1->setText(time);
}
 
 
void MyWidget::on_eventBtn_clicked()
{
    //判断按钮上的文本内容
    if(this->btn1->text() == "开始")
    {
        //启动一个定时器
        tid = this->startTimer(1000);    //启动定时器,并返回该定时器的id
        //该定时器启动后,会每隔1000毫秒,自动调用timerEvent函数
 
 
        //将按钮上的文件改成"关闭"
        this->btn2->setText("关闭");
    }else
    {
        //关闭定时器
        this->killTimer(tid);
 
 
        //将按钮上的文本设置成"开始"
        this->btn2->setText("开始");
    }
}
 
 
 
 
//定时器事件处理函数
void MyWidget::timerEvent(QTimerEvent *eent)
{
    //判断是哪个定时器到位
    if(eent->timerId() == tid)
    {
        //将系统的日期时间展示出来
        //调用系统的日期时间
        QDateTime sysDateTime = QDateTime::currentDateTime();
 
 
 
 
        //将日期时间转换为字符串
        QString dateTime = sysDateTime.toString("yyyy - MM - dd hh:mm:ss");
 
 
 
 
        //展示到ui界面中
        this->lab1->setText(dateTime);
    }
}
 
  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值