QT/QMessageBox/QTimerEvent/使用定时器制作一个闹钟

发布于:2024-06-24 ⋅ 阅读:(144) ⋅ 点赞:(0)

1.使用定时器制作一个闹钟

代码:

widget.cpp:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , speecher(new QTextToSpeech(this))//给语音对象申请空间
{
    ui->setupUi(this);
    this->setWindowTitle("闹钟");//设置窗口标题
     this->setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//设置窗口图标
    this->setWindowFlag(Qt::FramelessWindowHint);//隐藏窗口图标
    this->setAttribute(Qt::WA_TranslucentBackground);//去除多余空白部分

}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==id){
        //获取系统时间,并显示
        QTime sys_time=QTime::currentTime();//获取当前时间
        QString time_str=sys_time.toString("hh:mm:ss");//将获取的时间格式转为字符串
        ui->sys_time->setText(time_str);//显示到标签
        ui->sys_time->setAlignment(Qt::AlignCenter);//居中显示
    }
    if(e->timerId()==id2){
        for(int i=0;i<5;i++){//设置响铃次数
            speecher->say(ui->labtext->text());
        }
        killTimer(id2);//关闭闹钟
    }

}


void Widget::on_pushButton_clicked()
{
    static int SS;
    QString Str=ui->lineEdit->text();//获取行编辑器内容
    static int flag=0;
    if(flag%2==0){
    //QString Str=ui->lineEdit->text();//获取行编辑器内容
    //对字符串进行处理
    QStringList stringList=Str.split(":");//分割
    QString h=stringList[0];//小时
    QString m=stringList[1];//分钟
    QString s=stringList[2];//秒
    //将字符串转换为整数
    bool ok1,ok2,ok3;
    int H=h.toInt(&ok1,10);
    int M=m.toInt(&ok2,10);
    int S=s.toInt(&ok3,10);
    if(ok1==false||ok2==false||ok3==false||H>24||M>60||S>60){
        QMessageBox box;
        box.setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//自定义窗口图标
        box.setWindowTitle("闹钟设置不合法");
        box.setText("示例:小时:分钟:秒");
        box.addButton(QMessageBox::Ok);
        int retb=box.exec();//调用对话框
        if(retb==QMessageBox::Ok){
             ui->lineEdit->clear();//清空行编辑器内容
        }
    }else{
        //判断闹钟设置是否合法
        //qDebug() << H <<" " << M << " " << S;
        SS=H*60*60+M*60+S;//总秒数
        //弹出对话框,提示设置闹钟声音内容
        QMessageBox BOX;
        BOX.setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//自定义窗口图标
        BOX.setWindowTitle("提示");
        BOX.setText("设置闹钟文本");
        BOX.addButton(QMessageBox::Ok);
        int ret=BOX.exec();//调用对话框
        if(ret==QMessageBox::Ok){
            ui->lineEdit->setPlaceholderText("输入闹钟文本");//设置占位
            ui->lineEdit->clear();//清空行编辑器内容
        }
    }
    }else if(flag%2==1){
        //将行编辑内容放入到textlab中
        ui->labtext->setText(Str);
        ui->labtext->setAlignment(Qt::AlignLeft);//左对齐显示
        ui->lineEdit->setPlaceholderText("设置时间x:x:x");//设置占位
        ui->lineEdit->clear();//清空行编辑器内容
        //获取当前时间
        QTime sys_time=QTime::currentTime();//获取当前时间
        QString time_str=sys_time.toString("hh:mm:ss");//将获取的时间格式转为字符串
        //对字符串进行处理
        QStringList stringList=time_str.split(":");//分割
        QString hh=stringList[0];//小时
        QString mm=stringList[1];//分钟
        QString ss=stringList[2];//秒
        //将字符串转为整数
        bool ok;
        int HH=hh.toInt(&ok,10);
        int MM=mm.toInt(&ok,10);
        int Ss=ss.toInt(&ok,10);
        int TSS=HH*60*60+MM*60+Ss;
        //设置一个闹钟
        id2=startTimer((SS-TSS)*1000);

    }
    flag++;
}

main.cpp:

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.id=w.startTimer(1000);//设置一个定时器
    w.show();
    return a.exec();
}

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QDebug>
#include <QMessageBox>
#include <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);//重写定时器函数事件声明
     int id;//定时器id
     int id2;//闹钟定时器
     int id3;//定义闹钟响铃间隔时间
private slots:
     void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    QTextToSpeech *speecher;//定义语言成员
};
#endif // WIDGET_H

widget.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>458</width>
    <height>600</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <pointsize>9</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <property name="styleSheet">
   <string notr="true">*{
	
	background-color: rgb(255, 255, 255);
}
#frame{
	
	border-image: url(:/Logo/shanChuan.jpg);
}
#frame_2{
	
	background-color: rgba(186, 186, 186, 120);
	border-radius:10px;
}
#sys_time{
	background:transparent; /*完全透明*/
	font: 9pt &quot;宋体&quot;;
	color: rgba(255, 255, 255, 120);
}
#lineEdit{
	background:transparent; /*完全透明*/
	border:none;
	border-bottom:2px solid rgba(255,255,255,120);
	color: rgba(255, 255, 255, 120);
}
#pushButton{
	color: rgba(255, 255, 255, 120);
	background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(170, 255, 255, 255), stop:1 rgba(255, 255, 255, 120));
	font: 14pt &quot;宋体&quot;;
	border-radius:5px;
}
#labtext{
	
	background-color: rgba(217, 217, 217, 120);
	border-radius:5px;
	font: 12pt &quot;宋体&quot;;
	border-radius:5px;
}</string>
  </property>
  <widget class="QFrame" name="frame">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>20</y>
     <width>421</width>
     <height>561</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::StyledPanel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Raised</enum>
   </property>
   <widget class="QFrame" name="frame_2">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>30</y>
      <width>341</width>
      <height>501</height>
     </rect>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QLineEdit" name="lineEdit">
     <property name="geometry">
      <rect>
       <x>80</x>
       <y>150</y>
       <width>181</width>
       <height>41</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>13</pointsize>
      </font>
     </property>
     <property name="placeholderText">
      <string> 设置时间x:x:x</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton">
     <property name="geometry">
      <rect>
       <x>80</x>
       <y>220</y>
       <width>181</width>
       <height>41</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <family>宋体</family>
       <pointsize>14</pointsize>
       <weight>50</weight>
       <italic>false</italic>
       <bold>false</bold>
      </font>
     </property>
     <property name="text">
      <string>确认</string>
     </property>
    </widget>
    <widget class="QLabel" name="labtext">
     <property name="geometry">
      <rect>
       <x>40</x>
       <y>300</y>
       <width>261</width>
       <height>131</height>
      </rect>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
   </widget>
   <widget class="QLabel" name="sys_time">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>91</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

2.制作对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示登录成功,提供一个Ok按钮,用户点击OK后,关闭登录界面,跳转到其他界面;如果账号和密码不匹配,弹出错误对话框,给出信息"账号和密码不匹配,是否重新登录";并提供两个按钮Yes/No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则弹出一个问题对话框,给出信息"您是否确定要退出登录?“,并给出两个按钮Yes/No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能;要求:(基于属性版、静态成员函数都使用)实现对话框的弹出

代码:
widget.cpp:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("Shanghai");//设置窗口标题
    this->setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//设置窗口图标
    this->setWindowFlag(Qt::FramelessWindowHint);//隐藏窗口图标
    this->setAttribute(Qt::WA_TranslucentBackground);//去除多余空白部分
    connect(ui->pushButton_2,&QPushButton::clicked,this,&Widget::pushButton_2);//基于QT5版本,手动连接信号与槽,关闭按钮功能实现
    connect(ui->pushButton_3,&QPushButton::clicked,this,&Widget::pushButton_3);//基于QT5版本,手动连接信号与槽,最小化按钮功能实现
    connect(ui->lineEdit_2,&QLineEdit::editingFinished,this,&Widget::lineEdit_2);//获取密码,并进行相应处理

}

Widget::~Widget()
{
    delete ui;
}

void Widget::pushButton_2()
{
    this->close();//关闭窗口
}

void Widget::pushButton_3()
{
    this->showMinimized();//最小化
}
void Widget::lineEdit_2()
{
    if(ui->lineEdit_3->text()=="admin"&&ui->lineEdit_2->text()=="123456"){//判断登录账号密码是否一致
        QMessageBox BOX;
        //BOX.setFixedSize(400,800);//大小设置不生效
        BOX.setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//自定义窗口图标
        BOX.setWindowTitle("提示");
        BOX.setText("登录成功");
        BOX.addButton(QMessageBox::Ok);
        BOX.exec();//调用对话框
       // QMessageBox::information(this, "登录成功","");
        this->close();//关闭窗口
        emit widet_singnal();//激活信号函数,跳转下一个窗口
    }else{
        //对话框提示登录失败
        //QMessageBox::information(this, "登录失败","");
        QMessageBox BOX;
        BOX.setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//自定义窗口图标
        BOX.setWindowTitle("账户密码不匹配");
        BOX.setText("是否重新登录?");
        BOX.addButton(QMessageBox::Yes);
        BOX.addButton(QMessageBox::No);
        int ret=BOX.exec();//调用对话框
        if(ret==QMessageBox::Yes){
            ui->lineEdit_2->clear();
            ui->lineEdit_3->clear();//清空行编辑器
        }
        else{
            QMessageBox box;
            box.setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//自定义窗口图标
            box.setWindowTitle("提示");
            box.setText("您是否确认退出登录?");
            box.addButton(QMessageBox::Yes);
            box.addButton(QMessageBox::No);
            int ret1=box.exec();//调用对话框
            if(ret1==QMessageBox::Yes){
                this->close();
            }else{
                ui->lineEdit_2->clear();
                ui->lineEdit_3->clear();//清空行编辑器
            }
       }

    }
}

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setWindowTitle("Shanghai");//设置窗口标题
    this->setWindowIcon(QIcon(":/Logo/shangHai.jpg"));//设置窗口图标
    this->setWindowFlag(Qt::FramelessWindowHint);//隐藏窗口图标
    QMovie *mv=new QMovie(":/Logo/R-C.gif");//设置一个动态图对象指针接收动态图
    ui->label->setMovie(mv);//将动图设置到lab1标签中
    mv->start();//让动图动起来
    ui->label->setScaledContents(true);//让图片自适应标签设置大小

}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::dialog_slots()
{
    this->show();//打开另一个窗口
}

main.cpp

#include "widget.h"
#include "dialog.h"
#include <QApplication>
#include<QMessageBox>
#include<QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;//实例化一个窗口
    w.show();
    Dialog d;//实例化另一个窗口
    QObject::connect(&w,&Widget::widet_singnal,&d,&Dialog::dialog_slots);//两个窗口建立连接
    return a.exec();
}

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QIcon>
#include <QMovie>
namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();
public slots:
    void dialog_slots();//定义槽函数,用于两个窗口建立连接

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMessageBox>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
public slots:
    void pushButton_2();//x按钮槽函数
    void pushButton_3();//最小化槽函数
    void lineEdit_2();//密码行功能槽函数
signals:
    void widet_singnal();//定义信号函数,用于连接两个窗口
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>332</width>
    <height>376</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <property name="styleSheet">
   <string notr="true">*{/*&quot;*&quot;是通配符,匹配所有*/
	background-color: rgb(255, 255, 255);
	color: rgba(255, 255, 255, 120);
}
QFrame#frame{/*“#”也是通配符,匹配一个*/
	border-image: url(:/Logo/shanChuan.jpg);
}
#frame_2{
	background-color: rgba(179, 179, 179, 120);
}
#label{
	
	background-color: rgba(71, 71, 71, 120);
	border-radius:20px;
}
#label_2{
	background:transparent; /*完全透明*/
}
#lineEdit_2{
	background:transparent; /*完全透明*/
	border:none;
	border-bottom:1px solid rgba(255, 255, 255, 120);/*保留底部边框*/
	font: 10pt &quot;等线&quot;;
	color: rgba(255, 255, 255, 120);
}
#lineEdit_3{
	background:transparent; /*完全透明*/
	border:none;
	font: 10pt &quot;等线&quot;;
	border-bottom:1px solid rgba(255, 255, 255, 120);
	color: rgba(255, 255, 255, 120);
	font: 10pt &quot;等线&quot;;
}
#pushButton{
	font: 10pt &quot;等线&quot;;
	background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(58, 130, 205, 255), stop:1 rgba(255, 255, 255, 255));
	color: rgba(255, 255, 255, 120);
	border:none;
	border-radius:5px;
}
#pushButton:hover{/*鼠标移动*/
	font: 10pt &quot;等线&quot;;
	background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(80, 130, 205, 255), stop:1 rgba(255, 255, 255, 255));
	color: rgba(255, 255, 255, 120);
	border:none;
	border-radius:5px;
}
#pushButton:pressed{/*鼠标按下特效*/
	font: 10pt &quot;等线&quot;;
	background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(58, 130, 205, 255), stop:1 rgba(255, 255, 255, 255));
	color: rgba(255, 255, 255, 120);
	border:none;
	border-radius:5px;
	padding-top:5px;/*顶部抖动*/
    padding-left:5px;
}
#pushButton_2{
	background:transparent; /*完全透明*/
	color: rgba(255, 255, 255, 120);
	font: 10pt &quot;等线&quot;;
}
#pushButton_2:hover{
	background-color: rgb(255, 0, 0);
	color: rgba(255, 255, 255, 120);
	font: 10pt &quot;等线&quot;;
}
#pushButton_3{
	background:transparent; /*完全透明*/
	color: rgba(255, 255, 255, 120);
	font: 10pt &quot;等线&quot;;
}
#pushButton_3:hover{
	background-color: rgb(60, 239, 255);
	color: rgba(255, 255, 255, 120);
	font: 10pt &quot;等线&quot;;
}
#pushButton_4{
	background:transparent; /*完全透明*/
	color: rgba(255, 255, 255, 120);
	font: 10pt &quot;等线&quot;;
}
#pushButton_4:hover{
	background-color: rgb(60, 239, 255);
	color: rgba(255, 255, 255, 120);
	font: 10pt &quot;等线&quot;;
}</string>
  </property>
  <widget class="QFrame" name="frame">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>20</y>
     <width>221</width>
     <height>331</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::StyledPanel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Raised</enum>
   </property>
   <widget class="QFrame" name="frame_2">
    <property name="geometry">
     <rect>
      <x>-1</x>
      <y>-1</y>
      <width>231</width>
      <height>341</height>
     </rect>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QLabel" name="label">
     <property name="geometry">
      <rect>
       <x>30</x>
       <y>40</y>
       <width>161</width>
       <height>271</height>
      </rect>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
    <widget class="QLabel" name="label_2">
     <property name="geometry">
      <rect>
       <x>70</x>
       <y>60</y>
       <width>81</width>
       <height>41</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>15</pointsize>
      </font>
     </property>
     <property name="text">
      <string>Log In</string>
     </property>
    </widget>
    <widget class="QLineEdit" name="lineEdit_2">
     <property name="geometry">
      <rect>
       <x>50</x>
       <y>180</y>
       <width>121</width>
       <height>23</height>
      </rect>
     </property>
     <property name="echoMode">
      <enum>QLineEdit::Password</enum>
     </property>
     <property name="placeholderText">
      <string> Passwd</string>
     </property>
    </widget>
    <widget class="QLineEdit" name="lineEdit_3">
     <property name="geometry">
      <rect>
       <x>50</x>
       <y>140</y>
       <width>121</width>
       <height>23</height>
      </rect>
     </property>
     <property name="placeholderText">
      <string>Username</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton">
     <property name="geometry">
      <rect>
       <x>60</x>
       <y>240</y>
       <width>101</width>
       <height>24</height>
      </rect>
     </property>
     <property name="text">
      <string>Log In</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_2">
     <property name="geometry">
      <rect>
       <x>200</x>
       <y>0</y>
       <width>24</width>
       <height>24</height>
      </rect>
     </property>
     <property name="text">
      <string>X</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_3">
     <property name="geometry">
      <rect>
       <x>170</x>
       <y>0</y>
       <width>24</width>
       <height>24</height>
      </rect>
     </property>
     <property name="text">
      <string>-</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_4">
     <property name="geometry">
      <rect>
       <x>140</x>
       <y>0</y>
       <width>24</width>
       <height>24</height>
      </rect>
     </property>
     <property name="text">
      <string>?</string>
     </property>
    </widget>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

dialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>390</width>
    <height>303</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>230</x>
     <y>270</y>
     <width>161</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>391</width>
     <height>271</height>
    </rect>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>280</y>
     <width>211</width>
     <height>16</height>
    </rect>
   </property>
   <property name="text">
    <string>想欣赏妾身的舞姿吗</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonBox</sender>
   <signal>accepted()</signal>
   <receiver>Dialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>buttonBox</sender>
   <signal>rejected()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>


网站公告

今日签到

点亮在社区的每一天
去签到