本文實(shí)例為大家分享了Qt之鬧鐘小程序的具體代碼,供大家參考,具體內(nèi)容如下
-首先
首先我們利用Qt的designer 設(shè)計(jì)好我們需要的鬧鐘界面,設(shè)計(jì)界面如下圖:

其次我們來分別利用信號(hào)分別完成他們各自的槽函數(shù)
在mainwindow.h中,我們定義了下面這些私有成員變量,如下:
/ 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_OBJECTpublic: 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這些私有變量就是上述界面的元素指針,其種 QMediaPlayer 這個(gè)類用于播放mp3 媒體文件,用之前得在 .pro 文件中添加如下代碼:
QT += multimedia
這樣才能引入這個(gè)庫,接下來,我們開始在.cpp中完成各個(gè)槽函數(shù)。這里我們 得不斷檢測(cè)鬧鐘定時(shí)時(shí)間是否到達(dá)預(yù)設(shè)時(shí)間,我們必須得間隔500ms檢測(cè)一次,因此我們引入了定時(shí)器,QTimer,開啟之后,進(jìn)入循環(huán)檢測(cè)鬧鐘是否到點(diǎn)。
這里,我們選用復(fù)選框來設(shè)置鈴聲,當(dāng)然也可以改為下拉菜單的方式。
/ 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); //進(jìn)去后,失能開始 按鈕}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pushButton_clicked(){ myTimer->start(500); //star 按下,啟動(dòng)定時(shí)器 Temp = ui->timeEdit->time(); //獲取時(shí)鐘編輯器的值 ,為后續(xù) 系統(tǒng)時(shí)間的比較做準(zhǔn)備}void MainWindow::TimerResponse() //不斷檢查是否 定時(shí)時(shí)間到{ if (Temp.hour() == QTime::currentTime().hour() && Temp.minute() == QTime::currentTime().minute() ) //開始響鈴 { ui->label_2->setVisible(true); player->play(); myTimer->setSingleShot(true); //每次到點(diǎn)只能響鈴一次 }}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); // 重置后,有意可以為下次準(zhǔn)備響鈴 ui->label_2->setVisible(false); player->stop();}至此,小小的鬧鐘界面就完成了,很簡(jiǎn)單。但是對(duì)于了解Qt信號(hào)槽機(jī)制,很有幫助。同時(shí)使用了一個(gè)新類 QMediaPlayer 類。
最后效果如下所示:


這里,只加入了三首歌,我們可以新增復(fù)選框嗎,然后在之后的復(fù)選框的槽函數(shù)中加入和上述復(fù)選框的槽函數(shù)類似的代碼,增加新的音樂。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選