找回密码
 立即注册
收起左侧

定时器实现出错,咋回事?

3
回复
7243
查看
[复制链接]
累计签到:47 天
连续签到:1 天
来源: 2016-7-1 13:14:46 显示全部楼层 |阅读模式
1Qter豆
请问错误如何和解决???
错误:
myqt.cpp: In member function 'void Myqt:()':
myqt.cpp:33: error: redeclaration of 'QTimer* timer'
myqt.cpp:27: error: 'QTimer* timer' previously declared here

源码:
void Myqt:()
{

  while(1)
  {
           ledctl(1,0);
           QTimer *timer=new QTimer(this);
           connect(timer,SIGNAL(timeout()),this,SLOT(timeoutslot()));//timeoutslot()为自定义槽
           timer->start(1);


           ledctl(0,0);
           QTimer *timer=new QTimer(this);
           connect(timer,SIGNAL(timeout()),this,SLOT(timeoutslot()));//timeoutslot()为自定义槽
           timer->start(1);
  }
}



回复

使用道具 举报

累计签到:103 天
连续签到:1 天
2016-7-1 14:21:44 显示全部楼层
一个函数内怎么能定义两个名字相同的timer呢,你把下面的timer换一个名字,上面的提示告诉'QTimer* timer' previously declared here,之前定义过
回复

使用道具 举报

累计签到:103 天
连续签到:1 天
2016-7-1 14:22:39 显示全部楼层
还有你的定时器是想定义1ms?
回复

使用道具 举报

累计签到:615 天
连续签到:1 天
2016-7-1 23:36:01 显示全部楼层
这么写错误很大啊!
1.你每次循环,都会创建新的timer,然后绑定槽函数,这样效率低,很容易出错
2. qt 的 timer最多也就50ms,,,达不到1ms的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了