PDA

View Full Version : QTimer class and timeout() signal and slot not connecting



di_zou
8th September 2009, 22:11
I have a widget:


//foo.h
#include <QTimer>
...

class foo : public QWidget
{
...
private slots:
void on_timer_timeout();
...
private:
QTimer *timer;
...
}

and


//foo.cpp
#include "foo.h"
foo::foo(QWidget *parent)
: QWidget(parent)
{
...
timer = new QTimer(this);
timer->start(5);
...
}

void foo::on_timer_timeout()
{
//do stuff
}

When I run the program that creates the widget foo, I get the error:
QMetaObject::connectSlotsByName: No matching signal for on_timer_timeout().

What is the problem?

Boron
8th September 2009, 23:54
Don't you miss a line like this?
connect( timer, SIGNAL(timeout()), SLOT(on_timer_timeout()) );How shall the program know what to call when the signal timeout() is emitted?
And I have no idea what to do with you error message.

faldzip
9th September 2009, 01:26
the:


void on_xxx_yyy();

slots works fine but the xxx is not a C++ variable name but the object name (QObject::objectName(), QObject::setObjectName()), and didn't set any object name to your timer. So better do it in standard way, it means connect slot with signal manualy:


private slots:
void myTimeoutSlot();
private:
QTimer *timer;



. . .
connect(timer, SIGNAL(timeout()), this, SLOT(myTimeoutSlot()));
. . .