#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setStyleSheet("background-color: pink");
this->setFixedSize(540, 420);
time_label = new QLabel(this);
time_label->resize(280, 100);
time_label->setStyleSheet("background-color: white");
time_label->move(50, 25);
timer_id = this->startTimer(1000);
time_lineedit = new QLineEdit(this);
time_lineedit->resize(140, 60);
time_lineedit->setStyleSheet("background-color: white");
time_lineedit->move(time_label->x() + 300, time_label->y());
time_lineedit->setFont(QFont("Consolas", 14));
time_lineedit->setPlaceholderText("小时:分钟:秒数");
start_btn = new QPushButton("启动", this);
start_btn->resize(60, 30);
start_btn->move(time_label->x() + 300, time_label->y() + 70);
start_btn->setStyleSheet("background-color: white");
connect(start_btn, &QPushButton::clicked, this, &Widget::start_btn_clicked);
stop_btn = new QPushButton("停止", this);
stop_btn->resize(60, 30);
stop_btn->move(start_btn->x() + 80, time_label->y() + 70);
stop_btn->setStyleSheet("background-color: white");
stop_btn->setEnabled(false);
connect(stop_btn, &QPushButton::clicked, this, &Widget::stop_btn_clicked);
text_edit = new QTextEdit(this);
text_edit->resize(440, 280);
text_edit->setStyleSheet("background-color: white");
text_edit->move(50, time_label->y() + 110);
text_edit->setFont(QFont("Consolas", 20));
speecher = new QTextToSpeech(this);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Widget::timeout_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == timer_id)
{
// static int num = 0;
// ui->eventlabel->setNum(++num);
sys_dt = QDateTime::currentDateTime();
time_label->setText(sys_dt.toString("yyyy-MM-dd h:m:s"));
time_label->setAlignment(Qt::AlignCenter); //标签文本对齐方式
time_label->setFont(QFont("Consolas", 18));
}
}
void Widget::start_btn_clicked()
{
stop_btn->setEnabled(true);
text_edit->setEnabled(false);
time_lineedit->setEnabled(false);
start_btn->setEnabled(false);
timer->start(1000);
}
void Widget::stop_btn_clicked()
{
start_btn->setEnabled(true);
text_edit->setEnabled(true);
time_lineedit->setEnabled(true);
stop_btn->setEnabled(false);
timer->stop();
}
void Widget::timeout_slot()
{
QTime sys_time = QTime::currentTime();
QString time = sys_time.toString("h:m:s");
if(time == time_lineedit->text())
{
speecher->say(text_edit->toPlainText());
}
}
QT DAY 4
最新推荐文章于 2024-09-27 21:54:11 发布