程序想实现定时器1s发送数据,第一步程序运行自动发送,代码如下:
connect(&timer,SIGNAL(timeout()),this,SLOT(send()));
timer.start(1000);
程序运行没问题。
第二步,想通过按钮控制开始发送,代码如下:
QPushButton *send = new QPushButton(tr("Send"));
connect(send,SIGNAL(clicked()),&timer,SLOT(start(1000)));
connect(&timer,SIGNAL(timeout()),this,SLOT(send()));
程序运行报错:Object::connect: No such slot QTimer::start(1000)
将上面代码中start(1000),改为start(),即:
connect(send,SIGNAL(clicked()),&timer,SLOT(start()));
程序就不报错了,但不能实现定时发送了,这里就不明白差别在哪里呢?
后将程序改为:
timer.setInterval(1000);
connect(send,SIGNAL(clicked()),&timer,SLOT(start()));
connect(&timer,SIGNAL(timeout()),this,SLOT(send()));
实现了我要求的定时发送数据,但不明白为什么要这样??求各位指教