广州自助企业建站模板,淘宝运营培训多少钱,网站建设期末考试,上传到网站去的文档乱码做了一个自制的小闹钟#xff0c;能够自己输入时间#xff0c;以及对应的闹铃#xff0c;时间到了自动播放设定的闹铃#xff0c;可以随时取消重新设定#xff0c;采用分文件编译 注意#xff1a;需要在.pro文件中加入#xff1a;QT core gui texttospeech
代码… 做了一个自制的小闹钟能够自己输入时间以及对应的闹铃时间到了自动播放设定的闹铃可以随时取消重新设定采用分文件编译 注意需要在.pro文件中加入QT core gui texttospeech
代码如下 wideget.h #ifndef WIDGET_H
#define WIDGET_H#include QWidget
#include QKeyEvent //键盘事件类
#include QMouseEvent //鼠标事件类
#include QIcon
#include QPushButton
#include QLabel
#include QLineEdit
#include QMovie
#include QObject
#include QMessageBox
#include QTimer //定时器类
#include QTime //时间类
#include QTimerEvent //定时器事件类
#include QDateTime //日期时间类
#include QTextToSpeechQT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTsignals:public slots:void start_slots();void timeout_slots(); //自定义处理超时信号的槽函数void cancel_slots(); //自定义取消按钮槽函数public:Widget(QWidget *parent nullptr);~Widget();void mouseMoveEvent(QMouseEvent *event); //鼠标移动事件void mousePressEvent(QMouseEvent *event); //鼠标点击事件void speakText(const QString text);private:Ui::Widget *ui;QPoint temp; //移动窗口中间辅助向量QLabel *system_time,*lab1; //显示系统时间QLineEdit *mod_time,*clock_txt; //可编辑的时间、闹钟输出的文字QPushButton *start_button,*cancel_button; //启动按钮和取消按钮int tid 0; //定时器id号QTimer t1; //定义一个定时器变量};
#endif // WIDGET_Hmain.cpp #include widget.h#include QApplicationint main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}widget.cpp #include widget.h
#include ui_widget.h
#include QDebugWidget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui-setupUi(this);this-setFixedSize(500,500); //设置页面初始尺寸大小this-setWindowFlag(Qt::FramelessWindowHint); //将头部隐藏//标签类system_time new QLabel(this); //系统时间system_time-resize(200,140);system_time-move(50,50);system_time-setScaledContents(true);system_time-setStyleSheet(background-color:pink; border-radius:20;\font-size:20px;font-weight:bold);system_time-setWindowOpacity(0.1);//按钮类start_button new QPushButton(启动,this);cancel_button new QPushButton(取消,this);//设置位置start_button-move(system_time-x()system_time-width()15,110);cancel_button-move(start_button-x()start_button-width()5,start_button-y());//设置大小start_button-resize(90,80);cancel_button-resize(90,80);// start_button-setStyleSheet(border-radius:20);//行标签类mod_time new QLineEdit(this);mod_time-move(system_time-y()system_time-width()15,system_time-y());mod_time-setPlaceholderText(输入定时);mod_time-resize(180,30);clock_txt new QLineEdit(this);clock_txt-move(50,230);clock_txt-resize(400,250);clock_txt-setPlaceholderText(输入闹铃);// 设置文本对齐到左上角clock_txt-setAlignment(Qt::AlignLeft | Qt::AlignTop);clock_txt-setStyleSheet(QLineEdit { padding-left: 0px; });//连接信号与槽按下按钮开始执行功能connect(start_button, QPushButton::clicked, this, Widget::start_slots);connect(t1, QTimer::timeout, this, Widget::timeout_slots);connect(cancel_button, QPushButton::clicked, this, Widget::cancel_slots);
}Widget::~Widget()
{delete ui;
}//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{this-move(event-globalPos() - temp);
}//鼠标点击事件
void Widget::mousePressEvent(QMouseEvent *event)
{temp event-globalPos() - this-pos(); //求出中间辅助变量if(event-button() Qt::RightButton){this-close();}
}void Widget::start_slots()
{t1.start(1000); //每隔指定时间发送一个timeout信号
}void Widget::timeout_slots()
{//获取系统时间QTime sysTime QTime::currentTime();//将QTime类对象转变成字符串QString sysTimeStr sysTime.toString(hh:mm:ss);this-system_time-setText(sysTimeStr);system_time-setAlignment(Qt::AlignCenter);//将3个地方设置成不可点击start_button-setEnabled(false);mod_time-setReadOnly(true);clock_txt-setReadOnly(true);//比较逻辑如果和我输入的时间相等就发出声音QString modTimeStr mod_time-text();if(sysTimeStr modTimeStr){qDebug()发出声音;speakText(clock_txt-text());}
}void Widget::cancel_slots()
{int res QMessageBox::information(this,提示,真的要取消么,QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);if(res QMessageBox::Yes){qDebug()解除限制功能;t1.stop();start_button-setEnabled(true);mod_time-setReadOnly(false);clock_txt-setReadOnly(false);}else if(res QMessageBox::No){qDebug()继续执行程序;}
}void Widget::speakText(const QString text)
{QTextToSpeech *speaker new QTextToSpeech;speaker-say(text);
}
输出结果如下