Cannot connect Qtimer ?



  • Hi,
    I am trying to connect a Qtimer as follows:

    [code]
    QTimer *timer = new QTimer(this);
    unsigned short ret_val = QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->setInterval(100);
    timer->start();
    [/code]

    unfortunately the return value of connect is always "0" so false--> so connection to timer could not be established?
    what could be the problem?
    Any suggestions?



  • Hi,
    First of all the @QObject::connect()@ function returns a @QMetaObject::Connection@ variable, so maybe the automatic type casting is to blame, but doesn't sound likely.
    Does your this class subclass from QObject?? No need to use the @QObject::@ prefix for your connect, but that too (because the connect function is static) should not be to blame.
    The only thing I can think about is that your update() function takes arguments and the timeout() signal does not provide them. Then the connection will NOT be made. Also in QtCreator in your application output window a more detailed description will be given should the connection failed to be made.



  • Also, I believe update() has to be a
    public slot:


  • Moderators

    Hi,

    [quote author="downunderthunder" date="1391017218"][code]
    QTimer *timer = new QTimer(this);
    unsigned short ret_val = QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->setInterval(100);
    timer->start();
    [/code]

    unfortunately the return value of connect is always "0" so false--> so connection to timer could not be established?[/quote]The code you posted is in the correct format, except for "unsigned short" like Jerome pointed out.

    Did you run your code? Does update() get called?

    Anyway, if the connection fails, you will get an error message in your console. What does it say?

    [quote author="ScottR" date="1391027715"]Also, I believe update() has to be a
    public slot:[/quote]No it doesn't. The slot belongs to 'this', which is allowed to access all of its own slots (including private slots).



  • Well many thanks to all of you.

    The hint: "update" should be "public slot" help me somehow!

    I accidently declared it as "public" but indeed its a slot.
    now this part works fine and I can move forward...

    kind regards

    dut


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.