使用Qt制作简易的闹钟
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QMouseEvent>
#include <QDebug>
#include <QMessageBox>
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 *event);//重新定时器事件
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private slots:
void on_pushButton_3_clicked();
void on_pushButton_5_clicked();
void on_pushButton_4_clicked();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
bool showflag = true;
QPoint moverpoint;
QString clock_time_show = "00:00";//闹钟的显示时间
bool clock = false;//开启关闭闹钟
bool hmflag = true;//调整分钟还是小时
QString hint;
};
#endif // WIDGET_H
功能函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置固定大小
this->setFixedSize(800,350);
//去掉头部
this->setWindowFlag(Qt::FramelessWindowHint);
//定时器
startTimer(1000);
ui->lcdNumber_2->display(clock_time_show);
ui->textEdit->setVisible(0);//隐藏文本输入框
//隐藏设置闹钟的按键
ui->pushButton_3->setVisible(0);
ui->pushButton_4->setVisible(0);
ui->pushButton_5->setVisible(0);
//设定闹钟时间从现在时间开始
QTime systime = QTime::currentTime();
clock_time_show = systime.toString("hh:mm");
}
Widget::~Widget()
{
delete ui;
}
void Widget::mousePressEvent(QMouseEvent *event)
{
//检测鼠标左右键
if(event->buttons() == Qt::LeftButton)//左键拖动窗口
{
moverpoint = event->globalPos() - this->frameGeometry().topLeft();
}
if(event->buttons() == Qt::RightButton)//右键关闭窗口
{
QMessageBox::StandardButton btn = QMessageBox::information(this,"提示","确定退出吗?",QMessageBox::Yes|QMessageBox::No);//确认QMessageBox,防止误触
if(btn == QMessageBox::Yes)this->close();
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton)
{
this->move(event->globalPos() - moverpoint);//更改窗口的位置
}
}
void Widget::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
//时钟显示
QTime systime = QTime::currentTime();
QString timeText = systime.toString("hh:mm");
if(showflag)
{
timeText[2] = ':';
showflag = false;
}
else
{
timeText[2] = ' ';
showflag = true;
}
ui->lcdNumber->display(timeText);
//判断闹钟是否开启,时间是否到达
if(clock && clock_time_show == timeText)
{
ui->label->setText("提示的内容:"+hint);
QMessageBox::StandardButton btn = QMessageBox::information(this,"闹钟",clock_time_show+"到了",QMessageBox::Ok);
if(btn == QMessageBox::Ok)clock = false;;
}
}
void Widget::on_pushButton_3_clicked()
{
//设置闹钟时间+
int hour = clock_time_show.mid(0,2).toInt();
int mintue = clock_time_show.mid(3,2).toInt();
//判断调整分钟还是小时
if(hmflag == true)
{
mintue++;
if(mintue == 60)
mintue = 0;
QTime time(hour,mintue);
clock_time_show = time.toString("hh:mm");
}
else if(hmflag == false)
{
hour++;
if(hour == 24)
hour = 0;
QTime time(hour,mintue);
clock_time_show = time.toString("hh:mm");
}
//显示调整的时间
ui->lcdNumber_2->display(clock_time_show);
}
void Widget::on_pushButton_5_clicked()
{
//闹钟设定小时分钟切换
if(hmflag) hmflag=false;
else hmflag = true;
}
void Widget::on_pushButton_4_clicked()
{
int hour = clock_time_show.mid(0,2).toInt();
int mintue = clock_time_show.mid(3,2).toInt();
//设置闹钟时间-
if(hmflag == true)
{
mintue--;
if(mintue == 0)
mintue = 59;
QTime time(hour,mintue);
clock_time_show = time.toString("hh:mm");
}
else if(hmflag == false)
{
hour--;
if(hour == -1)
hour = 23;
QTime time(hour,mintue);
clock_time_show = time.toString("hh:mm");
}
ui->lcdNumber_2->display(clock_time_show);
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "设定闹钟")
{
//显示设置闹钟的按键
ui->pushButton_3->setVisible(1);
ui->pushButton_4->setVisible(1);
ui->pushButton_5->setVisible(1);
ui->textEdit->setVisible(1);//显示输入框
ui->pushButton->setText("确定");
ui->lcdNumber_2->display(clock_time_show);
}
else if(ui->pushButton->text() == "确定")
{
//确定闹钟
hint = ui->textEdit->document()->toPlainText();
clock = true;
ui->pushButton->setText("设定闹钟");
ui->textEdit->setVisible(0);
ui->label_2->setText("闹钟时间");
}
}
void Widget::on_pushButton_2_clicked()
{
clock = false;
ui->label_2->setText("当前没有闹钟");
ui->label->setText("");
}
普通界面
闹钟设定界面
闹钟时间到了提示