qt代码练习
计时器练习
namespace Ui {
class third;
}class third : public QWidget
{Q_OBJECTpublic: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);//labth1labth1->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();//labth2txtth1->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());}}
}
思维导图