• qt day 5


    1>实现闹钟功能

    1. ----------------------------------------------------------------------
    2. .pro
    3. ----------------------------------------------------------------------
    4. QT += core gui texttospeech
    5. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    6. CONFIG += c++11
    7. # The following define makes your compiler emit warnings if you use
    8. # any Qt feature that has been marked deprecated (the exact warnings
    9. # depend on your compiler). Please consult the documentation of the
    10. # deprecated API in order to know how to port your code away from it.
    11. DEFINES += QT_DEPRECATED_WARNINGS
    12. # You can also make your code fail to compile if it uses deprecated APIs.
    13. # In order to do so, uncomment the following line.
    14. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    15. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    16. SOURCES += \
    17. main.cpp \
    18. widget.cpp
    19. HEADERS += \
    20. widget.h
    21. # Default rules for deployment.
    22. qnx: target.path = /tmp/$${TARGET}/bin
    23. else: unix:!android: target.path = /opt/$${TARGET}/bin
    24. !isEmpty(target.path): INSTALLS += target
    25. ----------------------------------------------------------------------
    26. .h
    27. ----------------------------------------------------------------------
    28. #ifndef WIDGET_H
    29. #define WIDGET_H
    30. #include
    31. #include
    32. #include
    33. #include
    34. #include
    35. #include
    36. #include
    37. #include
    38. #include
    39. class Widget : public QWidget
    40. {
    41. Q_OBJECT
    42. public:
    43. Widget(QWidget *parent = nullptr);
    44. ~Widget();
    45. void Localtime();
    46. void Clock_check();
    47. void Opbtn_Cli();
    48. void Clobtn_Cli();
    49. void mousePressEvent(QMouseEvent *event) override;
    50. void mouseMoveEvent(QMouseEvent *event) override;
    51. private:
    52. //显示实施时间定时器
    53. QTimer *loct;
    54. QTimer *clotime;
    55. //界面组件
    56. QLabel *loctimlab;
    57. QLineEdit *clocklab;
    58. QPushButton *opbtn;
    59. QPushButton *clobtn;
    60. QTextEdit *txt;
    61. //时间比较
    62. int lhour = 0;
    63. int lmin = 0;
    64. int lsec = 0;
    65. int chour = 0;
    66. int cmin = 0;
    67. int csec = 0;
    68. //鼠标位置
    69. QPoint clipos;
    70. QPoint movpos;
    71. //播报员
    72. QTextToSpeech *sp = new QTextToSpeech(this);
    73. };
    74. #endif // WIDGET_H
    75. ----------------------------------------------------------------------
    76. main.cpp
    77. ----------------------------------------------------------------------
    78. #include "widget.h"
    79. #include
    80. int main(int argc, char *argv[])
    81. {
    82. QApplication a(argc, argv);
    83. Widget w;
    84. w.show();
    85. return a.exec();
    86. }
    87. ----------------------------------------------------------------------
    88. widget.cpp
    89. ----------------------------------------------------------------------
    90. #include "widget.h"
    91. Widget::Widget(QWidget *parent)
    92. : QWidget(parent)
    93. {
    94. this->setWindowFlag(Qt::FramelessWindowHint);
    95. this->resize(700,500);
    96. this->setStyleSheet("background-color:skyblue");
    97. this->setWindowOpacity(0.8);
    98. //创建显示当前时间文本框
    99. loctimlab = new QLabel(this);
    100. loctimlab->move(50,50);
    101. loctimlab->resize(350,100);
    102. loctimlab->setAlignment(Qt::AlignCenter);
    103. QFont ff;
    104. ff.setPointSize(20);
    105. loctimlab->setFont(QFont(ff));
    106. //创建定时时间文本框
    107. clocklab = new QLineEdit(this);
    108. clocklab->move(loctimlab->x()+loctimlab->width()+50,loctimlab->y());
    109. clocklab->resize(200,45);
    110. ff.setPointSize(16);
    111. clocklab->setAlignment(Qt::AlignHCenter);
    112. clocklab->setFont(QFont(ff));
    113. clocklab->setText("00 : 00 : 00");
    114. //创建开启按钮
    115. opbtn = new QPushButton(this);
    116. opbtn->move(clocklab->x(),clocklab->y()+clocklab->height()+10);
    117. opbtn->resize(75,45);
    118. opbtn->setText("开启");
    119. connect(opbtn,&QPushButton::clicked,this,&Widget::Opbtn_Cli);
    120. //创建停止按钮
    121. clobtn = new QPushButton(this);
    122. clobtn->move(clocklab->x()+opbtn->width()+50,clocklab->y()+clocklab->height()+10);
    123. clobtn->resize(75,45);
    124. clobtn->setText("停止");
    125. clobtn->setEnabled(0);
    126. connect(clobtn,&QPushButton::clicked,this,&Widget::Clobtn_Cli);
    127. //创建提示文本框
    128. txt = new QTextEdit(this);
    129. txt->move(loctimlab->x(),loctimlab->y()+loctimlab->height()+50);
    130. txt->resize(600,250);
    131. txt->setText("knocking!!!");
    132. //实例化定时器
    133. loct = new QTimer(this);
    134. loct->start(1000);
    135. clotime = new QTimer(this);
    136. //连接定时器与槽函数
    137. connect(loct,&QTimer::timeout,this,&Widget::Localtime);
    138. connect(clotime,&QTimer::timeout,this,&Widget::Clock_check);
    139. }
    140. Widget::~Widget()
    141. {
    142. }
    143. //获取实时时间
    144. void Widget::Localtime()
    145. {
    146. //获取实时时间
    147. QTime ltime = QTime::currentTime();
    148. //把时间显示到文本框上
    149. Widget::loctimlab->setText(ltime.toString("hh : mm : ss"));
    150. //获取时间
    151. lhour = ltime.hour();
    152. lmin = ltime.minute();
    153. lsec = ltime.second();
    154. }
    155. //时间比较
    156. void Widget::Clock_check()
    157. {
    158. if(lhour == chour && lmin == cmin && lsec == csec)
    159. {
    160. //阅读文本内容
    161. sp->say(txt->toPlainText());
    162. }
    163. }
    164. //开启闹钟定时
    165. void Widget::Opbtn_Cli()
    166. {
    167. //启动定时器
    168. clotime->start(1000);
    169. //获取定时时间
    170. QString tt;
    171. QByteArray t;
    172. QByteArray tmp;
    173. tt = clocklab->text();
    174. t = tt.toUtf8();
    175. int j = 0;
    176. //获取小时数
    177. for(int i = 0;t[j] != ':';i++,j++)
    178. {
    179. tmp[i] = t[j];
    180. }
    181. j++;
    182. chour = atoi(tmp);
    183. //获取分钟数
    184. for(int i = 0;t[j] != ':';i++,j++)
    185. {
    186. tmp[i] = t[j];
    187. }
    188. j++;
    189. cmin = atoi(tmp);
    190. //获取秒数
    191. for(int i = 0;t[j] != ':';i++,j++)
    192. {
    193. tmp[i] = t[j];
    194. }
    195. j++;
    196. csec = atoi(tmp);
    197. //设置其他为不可用
    198. opbtn->setEnabled(0);
    199. clocklab->setEnabled(0);
    200. txt->setEnabled(0);
    201. clobtn->setEnabled(1);
    202. }
    203. //停止按钮
    204. void Widget::Clobtn_Cli()
    205. {
    206. //设置按钮可用
    207. opbtn->setEnabled(1);
    208. clocklab->setEnabled(1);
    209. txt->setEnabled(1);
    210. clobtn->setEnabled(0);
    211. //停止定时器
    212. clotime->stop();
    213. }
    214. void Widget::mousePressEvent(QMouseEvent *event)
    215. {
    216. if(event->buttons() == Qt::LeftButton)
    217. {
    218. clipos = event->pos();
    219. }
    220. else
    221. {
    222. this->close();
    223. }
    224. }
    225. void Widget::mouseMoveEvent(QMouseEvent *event)
    226. {
    227. this->move(event->globalPos()-clipos);
    228. }

  • 相关阅读:
    【星球】【slam】研讨会 (3)ViSLAM 算法框架,原理,对比,评测 论文解读 ORB—SLAM3
    Blocking waiting for file lock on the registry index 问题解决
    Handler
    “ /^A-Z:\\{1,2}^/:\*\?<>\|+\.(jpg|gif|png|bmp)$/i ”这个正则表达式的理解
    .NET 反向代理 YARP 自定义配置提供程序(Configuration Providers)
    第2-4-10章 规则引擎Drools实战(3)-保险产品准入规则
    相交链表Java
    SpringCloud入门 1.Eureka Server安装 2.基础跨进程调用
    VScode常用快捷键
    Vue第1天:特性概览
  • 原文地址:https://blog.csdn.net/m0_64146298/article/details/132647660