QT实现简易闹钟功能

QT       += core gui texttospeech
******************************************
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
#include <QDebug>
#include <QTime>
#include <QTimer>
#include <QDateTime>
#include <QTextToSpeech>


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
    QLabel *label;
    QLineEdit *edit;
    QPushButton *btn1;
    QPushButton *btn2;
    QTextEdit *text;
    QTimer *timer;
     QTimer *timer2;
    QTextToSpeech *speecher;
    QLabel *back;

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

public slots:
    void btn1_slot();
    void btn2_slot();

private slots:
    void timeout_slot();
    void timeout_slot2();


private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
*********************************************************
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //取消头部,纯净窗口
    this->setWindowFlag(Qt::FramelessWindowHint);
    //qDebug()<<this->size();
    this->resize(QSize(600,400));
//       back = new QLabel(this);//设置背景照片
//       back->resize(this->size());
//       back->setPixmap(QPixmap(":/new/prefix1/C:/Users/30734/Desktop/zq/zq.png"));
//       back->setScaledContents(true);

    //实例一个显示时间标签
    label=new QLabel(this);
    //label->setStyleSheet("background-color:yellow");
    label->resize(280,65);
    label->move(50,50);

    //实例一个定时行编辑
    edit=new QLineEdit(this);
    edit->move(label->x()+label->width()+30,label->y());
    //qDebug() <<edit->size();
    edit->resize(QSize(150,25));
    edit->setAlignment(Qt::AlignCenter);
    edit->setStyleSheet("background: transparent;");

    //实例启动按钮
    btn1=new QPushButton(this);
    btn1->move(edit->x(),label->y()+edit->height()+15);
    //qDebug() <<btn1->size();
    btn1->resize(QSize(50,30));
    btn1->setText("启动");
    connect(btn1,&QPushButton::clicked,this,&Widget::btn1_slot);

    //实例停止按钮
    btn2=new QPushButton(this);
    btn2->move(btn1->x()+btn1->width()+50,btn1->y());
    btn2->resize(QSize(50,30));
    btn2->setText("关闭");
    btn2->setEnabled(false);
    connect(btn2,&QPushButton::clicked,this,&Widget::btn2_slot);

    //实例文本编辑
    text=new QTextEdit(this);
    text->move(label->x(),label->y()+label->height()+30);
    text->resize(QSize(500,215));
    text->setText("三更灯火五更鸡\n"
                  "正是男儿读书时\n"
                  "黑发不知勤学早\n"
                  "白首方悔读书迟\n");
    //text->setAlignment(Qt::AlignCenter);
    text->setFont(QFont("楷书",18));
    text->setStyleSheet("background: transparent;"); // 设置文本编辑器背景为透明

    //显示时间定时器
    timer = new QTimer(this);
    timer->start(1000);
    connect(timer,&QTimer::timeout,this,&Widget::timeout_slot);

    //语音播报
    speecher = new QTextToSpeech(this);

}

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

//显示时间函数
void Widget::timeout_slot()
{
    QTime sys_time = QTime::currentTime();
    QString t = sys_time.toString("hh:mm:ss");
    this->label->setText(t);
    this->label->setAlignment(Qt::AlignCenter);
    this->label->setFont(QFont("微软雅黑",35));
    this->label->setStyleSheet("color: white;");
}
//启动按钮槽函数
void Widget::btn1_slot()
{
    edit->setEnabled(false);
    text->setEnabled(false);
    btn1->setEnabled(false);
    btn2->setEnabled(true);

    timer2 = new QTimer(this);//启动后判断闹钟定时器
    timer2->start(1000);
    connect(timer2,&QTimer::timeout,this,&Widget::timeout_slot2);
}
//关闭按钮槽函数
void Widget::btn2_slot()
{
    btn2->setEnabled(false);
    btn1->setEnabled(true);
    edit->setEnabled(true);
    text->setEnabled(true);
    timer2->stop();//关闭闹钟

}
//判断闹钟函数
void Widget::timeout_slot2()
{
    if(edit->text()==label->text())
    {
        QString txt=text->toPlainText();
        speecher->say(txt);
    }

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在Qt实现一个简易的控制台,可以通过使用QPlainTextEdit类来模拟控制台的输出和输入功能。以下是一个简单的示例: ```cpp #include <QApplication> #include <QPlainTextEdit> #include <QKeyEvent> class Console : public QPlainTextEdit { public: Console(QWidget *parent = nullptr) : QPlainTextEdit(parent) { setReadOnly(true); // 设置为只读模式 setCursorWidth(0); // 隐藏光标 } protected: void keyPressEvent(QKeyEvent *event) override { if (event->key() == Qt::Key_Return) { QString command = getCurrentCommand(); appendPlainText("> " + command); // 将输入的命令显示在控制台上 // 在这里可以对输入的命令进行处理,比如执行相应的操作 clearCurrentCommand(); event->accept(); } else { QPlainTextEdit::keyPressEvent(event); } } private: QString getCurrentCommand() const { QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::StartOfLine); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); return cursor.selectedText().trimmed(); } void clearCurrentCommand() { QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::StartOfLine); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); cursor.removeSelectedText(); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Console console; console.show(); return a.exec(); } ``` 这个示例中,我们创建了一个自定义的Console类,继承自QPlainTextEdit。在keyPressEvent()函数中,我们捕获了回车键事件,获取当前输入的命令并显示在控制台上。您可以在这里对输入的命令进行处理,比如执行相应的操作。同时,我们通过重写getCurrentCommand()函数和clearCurrentCommand()函数来获取和清除当前输入的命令。 希望这个简单的示例对您有所帮助!如果您有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值