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

网站建设工作分解结构词典中国建筑管网平台

网站建设工作分解结构词典,中国建筑管网平台,全自动引流推广软件下载,保定比较好的网站建设公司本章要实现的整体效果如下: QT 中使用定时器,有两种方式: 定时器类:QTimer定时器事件:QEvent::Timer,对应的子类是 QTimerEvent 本节通过一个案例,同时讲解这两种方式 案例:当点击…

本章要实现的整体效果如下:

整体效果

QT 中使用定时器,有两种方式:

  • 定时器类:QTimer
  • 定时器事件:QEvent::Timer,对应的子类是 QTimerEvent

本节通过一个案例,同时讲解这两种方式

案例:当点击 “开始” 按钮,两个标签同时向右移动,下边标签的定时间隔是上边标签的 2 倍,因此移动慢。当两个标签移动到最右侧时,回到最右端循环移动,整体效果,如下:


1. 定时器事件 QTimerEvent

1.1 界面布局

把两个标签以及 “启动”、“停止”、“复位” 三个按钮布局在界面上。

首先,来到 timer_widget.h,声明两个标签:

#include <QLabel>class TimerWidget : public QWidget
{
private:QLabel* lbl1;QLabel* lbl2;
};

然后,来到 timer_widget.cpp ,添加两个标签:

#include <QPushButton>TimerWidget::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());
}

此时运行,点击按钮,效果如下:

timerEvent


2. 定时器类 QTimer

接下来,使用定时器类 QTimer 来实现以上同样的效果

首先,在 timer_widget.h 声明两个定时器类的对象,以及定时超时的槽函数:

#include <QTimer>class 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.yayakq.cn/news/977395/

相关文章:

  • 建设网站的企业专业服务郴州市人力资源考试网
  • 山东咕果做网站怎么样开发app外包公司
  • 定制企业网站php做的网站预览
  • 网站上线需要多久郑州网约车资格证
  • 网站制作设计机构网站建设产品需求文档
  • 建立自己网站免费wordpress做新闻cms
  • 推广网站最有效方法建一个网站报价
  • 怎样做移动端网站建网站赚钱
  • 廊坊网站公司vi设计公司排行
  • 做网站编写网页制作与设计在哪搜题
  • 网站开发 协作平台中国铁建门户网登录入口
  • wordpress 网站加载过慢如何网页制作
  • 服务器上的网站打不开做单页网站要多少钱
  • 网站建设 客户拜访ac68u做网站
  • 做网站用什么框架好百度seo查询系统
  • 影响网站权重的因素好看的html代码
  • 博客网站建设设计报告崇信县网站留言
  • 广安网站设计公司找人做彩票网站多少钱
  • 移动互联网开发方向要学什么久久seo正规吗
  • 建设商务网站作用湛江专业建站推广机构
  • 做项目的网站百度六安分公司
  • 福田皇岗社区网站建设临沂市网站建设
  • 电子类网站模板网站建设响应式
  • dede网站 远程生成佛山专业建设网站平台
  • 陕西新站seo重庆网站推广营销价格
  • 网站建设设置背景图片建设网站是做什么
  • 网站建设上机考试WordPress赞赏代码
  • 深圳网站设计公司哪个网站正在建设中下载
  • 如何看网站是否有做网站地图微信代理
  • 哪个网站可以做c 的项目哪个网站的地图可以做分析图