QT 一个简易闹钟

1 效果图

pro

QT       += core gui texttospeech

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    icon.qrc

.main

#include "widget.h"

#include <QApplication>

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

.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QIcon>   //
#include <QLabel>  //标签类
#include <QColor>  //颜色类
#include <QTimer>  //定时器类
#include <QTime>   //时间类
#include <QDateTime> //时间事件类
#include <QLineEdit> //行标签
#include <QPushButton>//按钮类
#include <QSize>
#include <QTextEdit>
#include <QMouseEvent> //鼠标事件
#include <QTextToSpeech>
#include <QString>
#include <QPalette>
#include <QImage>
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)override;

private slots:
    void alarm_start();  //自定义处理 闹钟启动 信号函数的声明
    void alarm_stop();   //自定义处理 停止闹钟 信号函数声明

private:
    Ui::Widget *ui;

    //将组件设置为私有成员
    QLabel *localtimelab;
    QLineEdit *alarm_edit;
    QPushButton *startbtn;
    QPushButton *stopbtn;
    QTextEdit *speakEdit;

    //获取闹铃时间
    QString alarm_time;
    int timer_id;    //定义一个定时器的id(基于事件处理函数)
    QTextToSpeech *speaker;//设置播报员
};
#endif // WIDGET_H

.cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //1、总体框架搭建
    this->setFixedSize(690,445);
    //2、设置窗口标题、Icon
    this->setWindowTitle("小桐闹铃");
    this->setWindowIcon(QIcon(":/icon/Alarm.jpg"));

    //3、组件设置
    //展示实时时间
    localtimelab = new QLabel(this);
    localtimelab->resize(400,100);
    localtimelab->move(30,30);
    localtimelab->setStyleSheet("QLabel{border:2px solid rgb(200,200,200);font-size:20px;}");
    localtimelab->setAlignment(Qt::AlignCenter);  //标签文本对齐

    //创建闹铃时间
    alarm_edit = new QLineEdit(this);
    alarm_edit->setPlaceholderText("请输入闹铃时间...");
    alarm_edit->resize(200,50);
    alarm_edit->move(localtimelab->width()+localtimelab->x()+30,localtimelab->y());
    alarm_edit->setStyleSheet("QLineEdit{border:2px solid rgb(200,200,200);}");
    alarm_edit->setAlignment(Qt::AlignCenter);

    //创建启动按钮
    startbtn = new QPushButton("启动",this);
    startbtn->resize(QSize(90,40));
    startbtn->move(alarm_edit->x(),alarm_edit->y()+60);
    connect(startbtn,&QPushButton::clicked,this,&Widget::alarm_start);    //将信号连接到槽函数

    //创建停止按钮
    stopbtn = new QPushButton("停止",this);
    stopbtn->resize(QSize(90,40));
    stopbtn->move(startbtn->x()+startbtn->width()+20,startbtn->y());
    stopbtn->setEnabled(false);
    connect(stopbtn,&QPushButton::clicked,this,&Widget::alarm_stop);    //将信号连接到槽函数

    //创建播报文本
    speakEdit = new QTextEdit(this);
    speakEdit->resize(630,250);
    speakEdit->move(localtimelab->x(),startbtn->y()+startbtn->height()+30);
    speakEdit->setPlaceholderText("请输入闹铃内容");
    speakEdit->setAlignment(Qt::AlignCenter);  //标签文本对齐

    //实例化一个播报员
    speaker = new QTextToSpeech(this);
    timer_id = this->startTimer(1000);
}

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

//启动闹铃的槽函数
void Widget::alarm_start()
{
    alarm_time = alarm_edit->text();  //将闹铃时间保存到alarm_time字符串中
    startbtn->setEnabled(false);
    alarm_edit->setEnabled(false);
    speakEdit->setEnabled(false);
    stopbtn->setEnabled(true);
}

//停止闹铃的槽函数
void Widget::alarm_stop()
{
    startbtn->setEnabled(true);
    alarm_edit->setEnabled(true);
    speakEdit->setEnabled(true);
    stopbtn->setEnabled(false);
}

//重写timerEvent函数
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == timer_id)
    {
        QDateTime local_time = QDateTime::currentDateTime();  //获取本地时间保存到local_time中
        localtimelab->setText(local_time.toString("yyyy-MM-dd hh:mm:ss"));
        if(alarm_time == local_time.toString("hh:mm:ss"))
        {
            speaker->say(speakEdit->toPlainText());
        }
    }
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简易的计算器示例,使用Qt实现: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建主窗口 QWidget window; window.setWindowTitle("Calculator"); // 创建文本框用于显示计算结果 QLineEdit* resultLineEdit = new QLineEdit(&window); resultLineEdit->setReadOnly(true); resultLineEdit->setAlignment(Qt::AlignRight); resultLineEdit->setFixedHeight(30); // 创建按钮布局 QGridLayout* buttonLayout = new QGridLayout; // 创建数字按钮 for (int i = 0; i < 10; ++i) { QPushButton* button = new QPushButton(QString::number(i), &window); buttonLayout->addWidget(button, i / 3, i % 3); QObject::connect(button, &QPushButton::clicked, [=]() { resultLineEdit->setText(resultLineEdit->text() + button->text()); }); } // 创建运算符按钮 QStringList operators = { "+", "-", "*", "/" }; for (int i = 0; i < operators.size(); ++i) { QPushButton* button = new QPushButton(operators.at(i), &window); buttonLayout->addWidget(button, i + 3, 3); QObject::connect(button, &QPushButton::clicked, [=]() { resultLineEdit->setText(resultLineEdit->text() + " " + button->text() + " "); }); } // 创建等号按钮 QPushButton* equalButton = new QPushButton("=", &window); buttonLayout->addWidget(equalButton, 6, 2); QObject::connect(equalButton, &QPushButton::clicked, [=]() { QString expression = resultLineEdit->text(); QScriptEngine engine; QScriptValue result = engine.evaluate(expression); resultLineEdit->setText(result.toString()); }); // 创建清除按钮 QPushButton* clearButton = new QPushButton("C", &window); buttonLayout->addWidget(clearButton, 6, 0); QObject::connect(clearButton, &QPushButton::clicked, [=]() { resultLineEdit->clear(); }); // 创建布局并设置主窗口布局 QVBoxLayout* mainLayout = new QVBoxLayout(&window); mainLayout->addWidget(resultLineEdit); mainLayout->addLayout(buttonLayout); // 显示主窗口 window.setLayout(mainLayout); window.show(); return app.exec(); } ``` 上述代码创建了一个简易的计算器界面,包括一个用于显示计算结果的文本框和数字、运算符按钮。用户可以通过点击按钮输入数字和运算符,并通过点击"="按钮执行计算并显示结果。"C"按钮用于清除文本框内容。 注意:该计算器使用Qt的QScriptEngine类来实现计算功能。它可以解析并计算基本的数学表达式。如果您需要更复杂的计算功能,可能需要使用其他的数学库或编写自己的计算逻辑。 希望这个示例对您有所帮助!如果您有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值