day4 c语言学习 c++之qt图形界面

今天写了一个闹钟小程序,功能是用户在界面右侧设定时间,设定时间后点击start按钮确定闹钟。当到了闹钟设定时间时候,会语音播报文本框中的内容。

期间除非按下close按钮,否则不允许改变文本框中内容,也不允许重新设定时间。按下则停止闹钟,可以重新设置

主要用到了qtimer,qmessagebox,信号与槽,qtexttospeech qpropertyanimation qmediaplayer

需要注意的是qtexttospeech我的电脑上无法播报中文,qmediaplayer播放的音乐有些压缩码率不支持,不会报错,但也不会播放音乐。目前发现支持的有128k。

头文件

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public slots:
    void get_cur_time();
    void start_timer();
    void count_alarm();
    void close_timer();

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private:

    //pre define child widgets 
    Ui::Widget *ui;
    QLineEdit *user_h;
    QLineEdit *user_m;
    QLineEdit *user_s;
    QLabel *cur_time;
    QLabel *lab_h;
    QLabel *lab_m;
    QLabel *rabbit_ani;
    QTextEdit *user_msg;
    QPushButton *start_btn;
    QPushButton *close_btn;
    QTimer *cur_timer;
    QTimer *ala_timer;
    QTime time;

    QTextToSpeech *speech;
    QPropertyAnimation *animation;
    QMediaPlayer *my_bgm;

};

#endif // WIDGET_H

主函数

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget主页面构造函数

widget.cpp

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

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    //set window size,title,icon
    ui->setupUi(this);
    this->setFixedSize(550,400);
    this->setWindowTitle("Alarm");
    this->setWindowIcon(QIcon(":/clock_32px.ico"));

    //open and read the qss file in the .qrc
    QFile file(":/Ubuntu.qss");
    file.open(QFile::ReadOnly);
    //store the file contents into a qstring
    QTextStream filetext(&file);
    QString stylesheet = filetext.readAll();
    //set style sheet with the qstring
    this->setStyleSheet(stylesheet);
    file.close();

    //set up the timer to indicate the current time
    cur_timer = new QTimer();
    cur_timer->start(10);
    connect(cur_timer,&QTimer::timeout,this,&Widget::get_cur_time);

    //set up the alarm timer to trigger the alarm
    ala_timer = new QTimer();
    connect(ala_timer,&QTimer::timeout,this,&Widget::count_alarm);

    //the current time is displayed in this label
    cur_time = new QLabel(this);
    cur_time->setGeometry(20,20,200,80);
    cur_time->setFont(QFont("楷体", 20, 5, true));
    cur_time->setAlignment(Qt::AlignCenter);
    cur_time->setStyleSheet("background-color:white;"
                            "border-radius:30px;");
    cur_time->setText("asdfasdfasdf");

    //the alarm time is set hh:mm:ss by user by the very three qlineeidts below
    //which are named respectively user_h user_m user_s
    user_h = new QLineEdit(this);
    user_h->setGeometry(280,20,50,40);
    user_h->setFont(QFont("楷体", 20, 5, true));
    user_h->setValidator( new QIntValidator(0, 24, this) );
    user_h->setStyleSheet("background-color:white;"
                            "border-radius:10px;");

    //a dummy label to display the charactor ":"
    lab_h = new QLabel(this);
    lab_h->setGeometry(340,20,20,40);
    lab_h->setFont(QFont("楷体", 20, 5, false));
    lab_h->setText(":");

    user_m = new QLineEdit(this);
    user_m->setGeometry(360,20,50,40);
    user_m->setFont(QFont("楷体", 20, 5, true));
    user_m->setValidator( new QIntValidator(0, 60, this) );
    user_m->setStyleSheet("background-color:white;"
                            "border-radius:10px;");
    
    //a dummy label to display the charactor ":"
    lab_m = new QLabel(this);
    lab_m->setGeometry(420,20,20,40);
    lab_m->setFont(QFont("楷体", 20, 5, false));
    lab_m->setText(":");

    user_s = new QLineEdit(this);
    user_s->setGeometry(440,20,50,40);
    user_s->setFont(QFont("楷体", 20, 5, true));
    user_s->setValidator( new QIntValidator(0, 60, this) );
    user_s->setStyleSheet("background-color:white;"
                            "border-radius:10px;");

    //the button to check the validation of the user input
    //and if valid, start the alarm timer
    start_btn = new QPushButton(this);
    start_btn->setGeometry(280,80,100,60);
    start_btn->setFont(QFont("楷体", 20, 5, false));
    start_btn->setText("Start");
    start_btn->setEnabled(true);
    connect(start_btn,&QPushButton::clicked,this,&Widget::start_timer);

    //the button to stop the alarm timer
    close_btn = new QPushButton(this);
    close_btn->setGeometry(400,80,100,60);
    close_btn->setFont(QFont("楷体", 20, 5, false));
    close_btn->setText("Close");
    close_btn->setEnabled(false);
    connect(close_btn,&QPushButton::clicked,this,&Widget::close_timer);

    //the content of which will be converted into voice by the time alarm triggered
    user_msg = new QTextEdit(this);
    user_msg->setGeometry(40,160,460,200);
    user_msg->setFont(QFont("楷体", 20, 5, false));
    user_msg->setStyleSheet("background-color:white;"
                            "border-radius:20px;");

    //this holds the moving rabbit image
    rabbit_ani = new QLabel(this);
    rabbit_ani->setPixmap(QPixmap(":/rabbit_48px.png"));

    //initialize QTextToSpeech
    speech = new QTextToSpeech(this);

    //animation initialization
    animation = new QPropertyAnimation(this);
    animation->setTargetObject(rabbit_ani);
    //the property of the animaiton is translation
    animation->setPropertyName("geometry");
    animation->setDuration(3000);
    //the rabbit should move back and forth along axis x from 0 to 200
    animation->setKeyValueAt(0, QRect(0, 100, 50, 50));
    animation->setKeyValueAt(0.5, QRect(200, 100, 50, 50));
    animation->setKeyValueAt(1, QRect(0, 100, 50, 50));
    animation->setLoopCount(-1);
    animation->start();

    //bgm playing
    my_bgm = new QMediaPlayer(this);
    my_bgm->setMedia(QUrl("qrc:/bgm.mp3"));
    my_bgm->setVolume(50);
    my_bgm->play();

}

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

void Widget::get_cur_time(){
    QString time = QTime::currentTime().toString();
    cur_time->setText(time);
}

void Widget::start_timer(){

    //check the validation from the input of the user
    //if it's a valid time then start the timer,enable/disable the relevant child widgets
    if(user_h->text().toInt() < 24 && user_h->text().toInt() >= 0 \
            && user_m->text().toInt() < 60 && user_m->text().toInt() >= 0 \
            && user_s->text().toInt() < 60 && user_s->text().toInt() >= 0)
    {

        time = QTime(user_h->text().toInt(),user_m->text().toInt(),user_s->text().toInt());
        qDebug()<start();
        user_h->setEnabled(false);
        user_m->setEnabled(false);
        user_s->setEnabled(false);
        user_msg->setEnabled(false);
        start_btn->setEnabled(false);
        close_btn->setEnabled(true);

    }
    //if not,display a warning message box
    else
    {
        int res = QMessageBox::warning(this, tr("Invalid Input"),
                                       tr("The time input is incorrect\n"
                                          "Do you want to modify it?"),
                                       QMessageBox::Ok | QMessageBox::Cancel);
        if(res == QMessageBox::Ok){
            user_h->clear();
            user_m->clear();
            user_s->clear();
        }
    }

}

void Widget::count_alarm(){

    //if the alarm set is equivalent to the current time
    //trigger the speecher,disable/enable the child widgets
    if(QTime::currentTime().toString() == time.toString()){
        ala_timer->stop();
        start_btn->setEnabled(true);
        close_btn->setEnabled(false);
        user_h->setEnabled(true);
        user_m->setEnabled(true);
        user_s->setEnabled(true);
        user_msg->setEnabled(true);
        speech->say(user_msg->toPlainText());
    }

}

void Widget::close_timer(){
    //if the alarm needs a reset
    //stop the counting alarm timer, disable/enable the child widgets
    ala_timer->stop();
    start_btn->setEnabled(true);
    close_btn->setEnabled(false);
    user_h->setEnabled(true);
    user_m->setEnabled(true);
    user_s->setEnabled(true);
    user_msg->setEnabled(true);
}

some screen shots:

day4 c语言学习 c++之qt图形界面_第1张图片

day4 c语言学习 c++之qt图形界面_第2张图片 

 

你可能感兴趣的:(学习,c++,c语言,qt,开发语言)