qt中用定时器实现闹钟
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTime>//时间类
#include<QTimer>//时间事件类
#include<QtTextToSpeech/QTextToSpeech>//文本转语音类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *e);//重写定时器事件函数声明
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
int id;//定时器id
int id2;
QTextToSpeech *speech;//实例化一个语音播报员
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
,speech(new QTextToSpeech(this))//实例化申请空间
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);//去掉头部分
this->setAttribute(Qt::WA_TranslucentBackground);//去掉空白部分
id = startTimer(1000);//启动计时器
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
//显示系统时间
QTime sys_time = QTime::currentTime();//获取当前系统时间
QString s = sys_time.toString("hh:mm:ss");//将系统时间转换成字符串
if(e->timerId() == id)
{
ui->label->setText(s);//将系统时间设置到ui界面上的label
ui->label->setAlignment(Qt::AlignCenter);//居中显示
}
if(s == ui->lineEdit->text())
{
killTimer(id2);
for(int i=0;i<5;i++)
{
speech->say(ui->label_3->text());
}
}
}
//x按钮槽函数
void Widget::on_pushButton_clicked()
{
this->close();//关闭窗口
}
//启动槽函数
void Widget::on_pushButton_2_clicked()
{
id2 = startTimer(1000);//启动定时器
}
运行
qt实现登录
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// 去掉头部
this->setWindowFlag(Qt::FramelessWindowHint);
// 去掉空白部分
this->setAttribute(Qt::WA_TranslucentBackground);
ui->lineEdit_2->setClearButtonEnabled(true);
};
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QMessageBox box;
//设置文本框的大小
if(ui->lineEdit->text() == "admin") {
if( ui->lineEdit_2->text() == "123456" ){
int ret = QMessageBox::information(this,"提示","登录成功",QMessageBox::Ok| QMessageBox::No);
if(ret == QMessageBox::Ok){
//登录界面关闭跳转下一个界面
emit my_jump_login();
}
}else{
QMessageBox::information(this,"提示","登录失败",QMessageBox::Ok);
}
}else{
QMessageBox::information(this,"提示","登录失败",QMessageBox::Ok);
}
}
void Widget::on_pushButton_2_clicked()
{
this->close();
}
void Widget::on_pushButton_3_clicked()
{
this->showMinimized();
}