How Qt implements an alarm clock applet

 

/mainwindow.h/**

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimeEdit>
#include <QTimer>
#include <QLabel>
#include <QMediaPlayer>
#include <QLineEdit>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT

public:
 explicit MainWindow(QWidget *parent = 0);
 ~MainWindow();

private slots:
 void TimerResponse();

 void on_pushButton_clicked();

 void on_pushButton_2_clicked();

 void on_radioButton_clicked();

 void on_radioButton_2_clicked();

 void on_radioButton_3_clicked();



 void on_pushButton_3_clicked();

private:
 Ui::MainWindow *ui;
 QTimeEdit *timeEdit;
 QLabel *label_2;
 QTime Temp;
 QLineEdit *lineEdit;
 QMediaPlayer *player = new QMediaPlayer;
 QTimer *myTimer = new QTimer(this);
};

#endif // MAINWINDOW_H

These private variables are the element pointers of the above interface. The class QMediaPlayer is used to play mp3 media files. Before using it, add the following code in the .pro file:

QT  += multimedia

Only in this way can the library be introduced. Next, we start to complete each slot function in .cpp. Here we have to constantly check whether the alarm clock timing has reached the preset time. We have to check every 500ms, so we introduce a timer, QTimer. After it is turned on, it enters a loop to check whether the alarm clock is on time.
Here, we choose the check box to set the ringtone, of course, it can also be changed to the drop-down menu.
/mainwindow.cpp/**

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <QTime>

int tt = 0;
MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent),
 ui(new Ui::MainWindow)
{

 ui->setupUi(this);
 ui->label_2->setVisible(false);
 QObject::connect(myTimer, SIGNAL(timeout()),
      this, SLOT(TimerResponse()) );
 ui->pushButton->setDisabled(true); //进去后,失能开始 按钮

}

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


void MainWindow::on_pushButton_clicked()
{

 myTimer->start(500);   //star 按下,启动定时器

 Temp = ui->timeEdit->time(); //获取时钟编辑器的值 ,为后续 系统时间的比较做准备

}

void MainWindow::TimerResponse() //不断检查是否 定时时间到
{
 if (Temp.hour() == QTime::currentTime().hour() &&
     Temp.minute() == QTime::currentTime().minute() )
  //开始响铃
 {
  ui->label_2->setVisible(true);
  player->play();
  myTimer->setSingleShot(true); //每次到点只能响铃一次
 }

}

void MainWindow::on_pushButton_2_clicked()
{
 tt++;
 if(tt == 10) tt = 0;
  else if(tt%2 == 1)
  player->play();
   else
    player->stop();
}



void MainWindow::on_radioButton_clicked()  //选中铃声1
{
 ui->pushButton->setEnabled(true);
 player->setVolume(30);
 player->setMedia(QUrl::fromLocalFile("C:/Users/Zhangkai/Desktop/Qt_Example/demo7/邱永传 - 伤心你的堕落.mp3"));
 ui->lineEdit->setText("邱永传 - 伤心你的堕落.mp3");
}

void MainWindow::on_radioButton_2_clicked() //选择铃声2
{
 ui->pushButton->setEnabled(true);
 player->setVolume(30);
 player->setMedia(QUrl::fromLocalFile("C:/Users/Zhangkai/Desktop/Qt_Example/demo7/邱永传 - 十一年.mp3"));
 ui->lineEdit->setText("邱永传 - 十一年.mp3");

}

void MainWindow::on_radioButton_3_clicked() //选择铃声3
{

 ui->pushButton->setEnabled(true);
 player->setVolume(30);
 player->setMedia(QUrl::fromLocalFile("C:/Users/Zhangkai/Desktop/Qt_Example/demo7/邱永传 - 十二年.mp3"));
 ui->lineEdit->setText("邱永传 - 十二年.mp3");

}

void MainWindow::on_pushButton_3_clicked()
{
 myTimer->setSingleShot(false); // 重置后,有意可以为下次准备响铃
 ui->label_2->setVisible(false);
 player->stop();
}

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/130583375