qt代码练习

该文章介绍了一个使用Qt框架实现的计时器应用,包括QTimer事件处理、QLabel显示时间、QTextEdit设定提醒文本、QLineEdit输入设置、QPushButton控制计时器开关,以及QTextToSpeech的语音提醒功能。
摘要由CSDN通过智能技术生成

计时器练习

namespace Ui {
class third;
}

class third : public QWidget
{
    Q_OBJECT

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

    QLabel *labth1= new QLabel(this);
    QTextEdit *txtth1= new QTextEdit("闹钟",this);

    QLineEdit *leth1 = new QLineEdit(this);

    QPushButton *btnth1 = new QPushButton("开启",this);
    QPushButton *btnth2 = new QPushButton("关闭",this);

    QTimer *timeth = new QTimer(this);


private:
    Ui::third *ui;
    int tid1;
    int tid2;

    void timerEvent(QTimerEvent *e);
    void timerout(QTimerEvent *eo);

    QTextToSpeech *speecher;

signals:
    void back();
    void nowtime();
//    void outtime();
//    void spk();
public slots:
    void thjump_slot();
//    void out_time();
//    void my_spk();
    //定时器开关信号
    void btn_open();
    void btn_close();
};

cpp部分

third::third(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::third)
{
    ui->setupUi(this);
    this->setFixedSize(800,500);
    this->setWindowTitle("Irenaの小屋");
    this->setWindowIcon(QIcon(":/icon/kirin1.png"));
    this->setStyleSheet("background-color:white;");
    this->setWindowOpacity(0.95);

    //labth1
    labth1->resize(150,60);
    labth1->move(100,30);
    labth1->setAlignment(Qt::AlignCenter);
    QFont size20;
    size20.setPointSize(20);
    labth1->setFont(size20);
    timerEvent(0);
    startTimer(1000);
    labth1->show();
    //labth2
    txtth1->resize(150,60);
    txtth1->move(200,150);

    //按钮——开启
    btnth1->move(350,80);
    btnth1->resize(50,30);
    btnth1->setIcon(QIcon(":/icon/king2.png"));

    btnth2->move(420,80);
    btnth2->resize(50,30);
    btnth2->setIcon(QIcon(":/icon/king2.png"));

    connect(btnth1,&QPushButton::clicked,this,&third::btn_open);
    connect(btnth2,&QPushButton::clicked,this,&third::btn_close);

    connect(timeth,SIGNAL(outtime()),this,SLOT(out_time()));


    //行编辑
    leth1->resize(140,30);
    leth1->move(330,45);

    speecher = new QTextToSpeech(this);

}


//菜单栏跳转
void third::thjump_slot()
{
    this->show();
}

//定时器开启
void third::btn_open()
{
    btnth1->setEnabled(false);
    tid2 = startTimer(500);
}

void third::btn_close()
{
    btnth1->setEnabled(false);
    leth1->clear();
    this->killTimer(tid2);

}
void third::timerEvent(QTimerEvent *e)
{
    Q_UNUSED(e);
    labth1->setText(QTime::currentTime().toString("hh:mm:ss"));


}

void third::timerout(QTimerEvent *eo)
{
    if(eo->timerId() == tid2)
    {
        if(leth1->text() == labth1->text() )
        {
            speecher->say(txtth1->toPlainText());
        }
    }
}

思维导图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值