QT---day4事件

ops/2025/2/7 3:39:24/

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);
    }
}
 

http://www.ppmy.cn/ops/38700.html

相关文章

Python turtle库 实现 随机彩色文字平面批量输出

# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ import turtle import random import turtle as t t.colormode(255) turtle.bgcolor("white") h255 l50#字号 m60#间隔 n500 t.penup() turtle.hide…

基于STM32的宠物箱温度湿度监控系统毕业设计

基于STM32的宠物箱温度湿度监控系统毕业设计 一、项目背景与意义 随着人们生活水平的提高&#xff0c;养宠物已经成为一种流行趋势。然而&#xff0c;对于宠物的居住环境&#xff0c;尤其是温度与湿度的控制&#xff0c;是确保宠物健康的关键。本项目旨在设计一款基于STM32微…

Node.js爬虫在租房信息监测与分析中的应用

在当今数字化时代&#xff0c;房地产市场的信息变化迅速&#xff0c;租房信息的获取和分析对于租房者和房东都至关重要。随着互联网技术的发展&#xff0c;利用爬虫技术来监测和分析租房信息已成为一种常见的做法。本文将探讨如何利用Node.js爬虫在租房信息监测与分析中的应用前…

最新优质电商API接口,附带教程【多语言环境高并发】

给大家更新一波24年一月份的新接口吧。 01 接口信息 线路推荐: 多仓&#xff1a; 1.春盈&#xff1a; https://wds.ecsxs.com/230989.json 2.无意&#xff1a; http://www.wya6.cn/tv/yc.json 3.主流电商平台API数据采集 单仓&#xff1a; 1.饭太硬&#xff1a; http:/…

什么是股指期货风险度?

期货风险度就像是你账户的“健康指标”&#xff0c;它告诉我们你用了多少资金来持有期货合约&#xff0c;以及你账户里还剩下多少“备用金”。风险度越高&#xff0c;意味着你的“备用金”越少&#xff0c;如果市场突然变化&#xff0c;你可能需要迅速补充资金。 股指期货风险…

第七十六章 Apache 注意事项 (UNIX® Linux macOS)

文章目录 第七十六章 Apache 注意事项 (UNIX Linux macOS)Apache 流程管理和容量规划安全Apache MPM 和 Web Gateway DSO最大服务器连接数 第七十六章 Apache 注意事项 (UNIX Linux macOS) 本页包含有关 UNIX、Linux 和 macOS 的推荐选项&#xff08;推荐选项&#xff1a;NSAP…

Android 蓝牙实战——蓝牙电话通话状态分析(二十三)

在前面的《Android 蓝牙——HFP协议(九)》中我们知道了蓝牙电话主要使用的是 HFP 协议,这里我们主要分析一些客户端如何获取蓝牙电话状态的变化。 一、电话状态查询 1、HeadsetClientStateMachine 源码位置:/packages/apps/Bluetooth/src/com/android/bluetooth/hfpclie…

2024年自动驾驶、车辆工程与智能交通国际会议(ICADVEIT2024)

2024年自动驾驶、车辆工程与智能交通国际会议&#xff08;ICADVEIT2024&#xff09; 会议简介 2024年自动驾驶、车辆工程和智能交通国际会议&#xff08;ICADVEIT 2024&#xff09;将在中国深圳举行。会议主要聚焦自动驾驶、车辆工程和智能交通等研究领域&#xff0c;旨在为从…