当前位置: 首页 > news >正文

树莓派做博客网站深圳网站建设与网站制作

树莓派做博客网站,深圳网站建设与网站制作,影视公司排名,湛江做建站软仿本章要实现的整体效果如下#xff1a; QT 中使用定时器#xff0c;有两种方式#xff1a; 定时器类#xff1a;QTimer定时器事件#xff1a;QEvent::Timer#xff0c;对应的子类是 QTimerEvent 本节通过一个案例#xff0c;同时讲解这两种方式 案例#xff1a;当点击…本章要实现的整体效果如下 QT 中使用定时器有两种方式 定时器类QTimer定时器事件QEvent::Timer对应的子类是 QTimerEvent 本节通过一个案例同时讲解这两种方式 案例当点击 “开始” 按钮两个标签同时向右移动下边标签的定时间隔是上边标签的 2 倍因此移动慢。当两个标签移动到最右侧时回到最右端循环移动整体效果如下 1. 定时器事件 QTimerEvent 1.1 界面布局 把两个标签以及 “启动”、“停止”、“复位” 三个按钮布局在界面上。 首先来到 timer_widget.h声明两个标签 #include QLabelclass TimerWidget : public QWidget { private:QLabel* lbl1;QLabel* lbl2; };然后来到 timer_widget.cpp 添加两个标签 #include QPushButtonTimerWidget::TimerWidget(QWidget* parent) : QWidget{parent} {QVBoxLayout* verticalLayout new QVBoxLayout(this);verticalLayout-setSpacing(0);verticalLayout-setContentsMargins(0, 0, 0, 0);// 1. 添加第一个QLabellbl1 new QLabel(this);lbl1-setText();lbl1-setFrameShape(QFrame::Box);lbl1-setFixedSize(100, 100);lbl1-setStyleSheet(background-color: red;);verticalLayout-addWidget(lbl1);// 2. 添加第二个QLabellbl2 new QLabel(this);lbl2-setText();lbl2-setFrameShape(QFrame::Box);lbl2-setFixedSize(100, 100);lbl2-setStyleSheet(background-color: blue;);verticalLayout-addWidget(lbl2);// 3. 添加按钮的水平布局QHBoxLayout* horizontalLayout new QHBoxLayout(this);horizontalLayout-setSpacing(0);horizontalLayout-setContentsMargins(0, 0, 0, 0);QPushButton* btnStart new QPushButton(this);btnStart-setText(开始);horizontalLayout-addWidget(btnStart);QPushButton* btnStop new QPushButton(this);btnStop-setText(停止);horizontalLayout-addWidget(btnStop);QPushButton* btnReset new QPushButton(this);btnReset-setText(复位);horizontalLayout-addWidget(btnReset);verticalLayout-addLayout(horizontalLayout);this-setStyleSheet(R(QPushButton {font-size: 22px;})); }此时运行程序效果如下 1.2 关联信号槽 首先来到 timer_widget.h声明 3 个槽函数 class TimerWidget : public QWidget { private slots:void onStartClicked();void onStopClicked();void onResetClicked(); };然后来到 timer_widget.cpp做一个空实现 void TimerWidget::onStartClicked() { }void TimerWidget::onStopClicked() { }void TimerWidget::onResetClicked() { }最后来到 timer_widget.cpp在构造中连接信号槽 TimerWidget::TimerWidget(QWidget* parent) : QWidget{parent} {// ...connect(btnStart, QPushButton::clicked, this, TimerWidget::onStartClicked);connect(btnStop, QPushButton::clicked, this, TimerWidget::onStopClicked);connect(btnReset, QPushButton::clicked, this, TimerWidget::onResetClicked); }1.3 重写 timerEvent 当定时时间到就会自动调用 timerEvent() 函数。 首先来到 timer_widget.h 中声明 timerEvent() 函数并声明两个定时器 id class TimerWidget : public QWidget { protected:void timerEvent(QTimerEvent* event);private:int id1;int id2; };然后来到 timer_widget.cpp 中实现 timerEvent() 函数 void TimerWidget::timerEvent(QTimerEvent* event) {// qDebug() timerEvent;if ( event-timerId() id1 ) {lbl1-move(lbl1-x() 5, lbl1-y());if ( lbl1-x() this-width() ) {lbl1-move(0, lbl1-y());}} else if ( event-timerId() id2 ) {lbl2-move(lbl2-x() 5, lbl2-y());if ( lbl2-x() this-width() ) {lbl2-move(0, lbl2-y());}} }说明 在 timerEvent() 函数中向右移动标签。当标签超出当前窗口重新回到最左侧可以启动多个定时器在 timerEvent() 中使用 QTimerEvent 类的 timerId() 函数可以获取哪个定时器定时时间到 1.4 实现槽函数启动定时器 上一步中提到定时时间到时移动标签那么就需要先启动定时器并指定一个定时间隔。 首先在 “启动” 按钮的槽函数中启动定时器 void TimerEventWidget::onStartClicked() {id1 startTimer(10); // 时间间隔10msid2 startTimer(20); // 时间间隔20ms }然后在 “停止” 按钮的槽函数中停止定时器 void TimerEventWidget::onStopClicked() {killTimer(id1);killTimer(id2); }最后在 “复位” 按钮的槽函数中复位标签位置到最左侧 void TimerEventWidget::onResetClicked() {lbl1-move(0, lbl1-y());lbl2-move(0, lbl2-y()); }此时运行点击按钮效果如下 2. 定时器类 QTimer 接下来使用定时器类 QTimer 来实现以上同样的效果 首先在 timer_widget.h 声明两个定时器类的对象以及定时超时的槽函数 #include QTimerclass TimerWidget : public QWidget { private slots:void onTimeout1();void onTimeout2();private:QTimer* timer1;QTimer* timer2; };然后在 timer_widget.cpp 中实现两个定时超时槽函数 void TimerWidget::onTimeout1() {lbl1-move(lbl1-x() 5, lbl1-y());if ( lbl1-x() this-width() ) {lbl1-move(0, lbl1-y());} }void TimerWidget::onTimeout2() {lbl2-move(lbl2-x() 5, lbl2-y());if ( lbl2-x() this-width() ) {lbl2-move(0, lbl2-y());} }这里移动标签并在标签超出当前窗口边界时复位到最左侧 接着修改 “启动”、“停止” 按钮的槽函数。 为便于切换定时器类和定时器事件这两种方式定义了一个宏 #define USE_TIMER_EVENTvoid TimerWidget::onStartClicked() { #ifdef USE_TIMER_EVENTid1 startTimer(10);id2 startTimer(20); #elsetimer1-start(20);timer2-start(10); #endif }void TimerWidget::onStopClicked() { #ifdef USE_TIMER_EVENTkillTimer(id1);killTimer(id2); #elsetimer1-stop();timer2-stop(); #endif }最后在 timer_widget.cpp 的构造中创建定时器并关联槽函数 TimerWidget::TimerWidget(QWidget* parent) : QWidget{parent} {// ...timer1 new QTimer(this);connect(timer1, QTimer::timeout, this, TimerWidget::onTimeout1);timer2 new QTimer(this);connect(timer2, QTimer::timeout, this, TimerWidget::onTimeout2); }此时运行效果如下
文章转载自:
http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn
http://www.morning.csdgt.cn.gov.cn.csdgt.cn
http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn
http://www.morning.snkry.cn.gov.cn.snkry.cn
http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn
http://www.morning.zffn.cn.gov.cn.zffn.cn
http://www.morning.trzzm.cn.gov.cn.trzzm.cn
http://www.morning.pdynk.cn.gov.cn.pdynk.cn
http://www.morning.txjrc.cn.gov.cn.txjrc.cn
http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn
http://www.morning.wpjst.cn.gov.cn.wpjst.cn
http://www.morning.xnpml.cn.gov.cn.xnpml.cn
http://www.morning.fcwb.cn.gov.cn.fcwb.cn
http://www.morning.phechi.com.gov.cn.phechi.com
http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn
http://www.morning.rhmk.cn.gov.cn.rhmk.cn
http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn
http://www.morning.gtqws.cn.gov.cn.gtqws.cn
http://www.morning.pdmc.cn.gov.cn.pdmc.cn
http://www.morning.nfqyk.cn.gov.cn.nfqyk.cn
http://www.morning.npbnc.cn.gov.cn.npbnc.cn
http://www.morning.rrrrsr.com.gov.cn.rrrrsr.com
http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn
http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn
http://www.morning.iiunion.com.gov.cn.iiunion.com
http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn
http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn
http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn
http://www.morning.ccphj.cn.gov.cn.ccphj.cn
http://www.morning.kqzt.cn.gov.cn.kqzt.cn
http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn
http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn
http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com
http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn
http://www.morning.kehejia.com.gov.cn.kehejia.com
http://www.morning.ysskn.cn.gov.cn.ysskn.cn
http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn
http://www.morning.bfycr.cn.gov.cn.bfycr.cn
http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn
http://www.morning.fmqw.cn.gov.cn.fmqw.cn
http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn
http://www.morning.xblrq.cn.gov.cn.xblrq.cn
http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn
http://www.morning.gqbks.cn.gov.cn.gqbks.cn
http://www.morning.ctbr.cn.gov.cn.ctbr.cn
http://www.morning.rggky.cn.gov.cn.rggky.cn
http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn
http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn
http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn
http://www.morning.bpttm.cn.gov.cn.bpttm.cn
http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn
http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn
http://www.morning.kryxk.cn.gov.cn.kryxk.cn
http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn
http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn
http://www.morning.ssjee.cn.gov.cn.ssjee.cn
http://www.morning.znqfc.cn.gov.cn.znqfc.cn
http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn
http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn
http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn
http://www.morning.thpns.cn.gov.cn.thpns.cn
http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn
http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn
http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn
http://www.morning.lkthj.cn.gov.cn.lkthj.cn
http://www.morning.nzmw.cn.gov.cn.nzmw.cn
http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn
http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn
http://www.morning.weiwt.com.gov.cn.weiwt.com
http://www.morning.phxns.cn.gov.cn.phxns.cn
http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn
http://www.morning.rjznm.cn.gov.cn.rjznm.cn
http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn
http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn
http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn
http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn
http://www.morning.gthgf.cn.gov.cn.gthgf.cn
http://www.morning.mhnd.cn.gov.cn.mhnd.cn
http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn
http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn
http://www.tj-hxxt.cn/news/247263.html

相关文章:

  • 做网站有维护费是什么费用开发网站和application
  • 网站 廉政建设 板块购物网站的开发
  • 小学网站建设方案淄博网站制作高端营销
  • 哪些网站是jsp做的wordpress点赞打赏
  • asp做的网站js网站开发工具
  • 四线城市做网站建设怎么样哪儿有网络推广培训
  • 济南网站制作报价中博建设集团有限公司网站
  • 交流平台网站怎么做制作网站的登录界面怎么做
  • 郴州做网站的公司如何创立自己的品牌
  • 福建省建设工程信息网站win 7怎么卸载wordpress
  • 沛县做网站xlec吉林品牌网站建设商家
  • 网站建设公司2018-10-1页面设计结课总结
  • 源码开发网站建设asp网站发邮件
  • 浅谈网站建设的目的和意义网站开发用的开源系统
  • 公司网站集资网站开发人员犯法么平台网站建设网站
  • 游戏网站的设计品牌网址是什么
  • 怎么用自己的电脑搭建网站精美的php个人网站源码
  • 网站设计制作哪家服务好中国建设银行的网站用户名是什么意思
  • 安康网站建设公司有哪些公司网站做一年多少钱
  • 建设网站增城网站突然不收录了
  • 网站适配手机如何把代码wordpress
  • 织梦移动端网站模板下载地址资阳seo
  • 灵璧县建设局网站设计灵感网站整理
  • 简述jsp网站架构wordpress 页面调新闻
  • 咸宁网站建设哪家好售后服务网点建设是指网站
  • 建设网站虚拟主机网站界面设计技巧
  • 网站开发公司 杭州潍坊网站搜索引擎优化
  • 网站开发实训新的体会深圳市市长
  • 网站建设的市场策划宁波网站建设有限公司
  • 上海网站推广服务wordpress侧边栏加入广告