头文件 #include <cmath>
#include <QApplication>
#include <QMainWindow>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
#include <QRandomGenerator>
#include <QTimerEvent>
#include <QTextToSpeech>
namespace Ui {
class Disan;
}
class Disan : public QWidget
{
Q_OBJECT
public:
explicit Disan(QWidget *parent = nullptr);
~Disan();
private:
Ui::Disan *ui;
int id;
QTextToSpeech sp;
public slots:
void my_slots();
void timerEvent(QTimerEvent *e);
private slots:
void on_pushButton_clicked();
};
主文件
void Disan::timerEvent(QTimerEvent *e)
{
QTime syetime=QTime::currentTime();
QString t=syetime.toString("hh-mm-ss");
ui->label_2->setText(t);
ui->label_2->setAlignment(Qt::AlignCenter);
if(ui->lineEdit->text()==ui->label_2->text())
{
ui->label_5->setVisible(true);
ui->label_4->setVisible(true);
}
else
{
ui->label_5->setVisible(false);
ui->label_4->setVisible(false);
}
}
Disan::~Disan()
{
delete ui;
}
void Disan::my_slots()
{
QTimer *timer = new QTimer(this);
// 连接 QTimer 的 timeout 信号到槽函数
connect(timer, &QTimer::timeout, this, &Disan::show);
// 设置定时器在1秒钟后触发
timer->start(7000);
}
void Disan::on_pushButton_clicked()
{
id=startTimer(1000);
qDebug() << ui->lineEdit->text();
qDebug() << ui->label_2->text();
}
#endif // DISAN_H