PDA

View Full Version : Problem with QTimer : the function that would be load is not load!



lifedj
3rd March 2013, 20:56
Hi, I have a problem in using QTimer.
I'm trying to use this piece of code, but I don't know why the function "cameraTimerTimeout()" doesn't start:

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <iostream>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();
void cameraTimerTimeout();
void cattura();

private:
Ui::MainWindow *ui;
QTimer cameraTimer;
};

#endif // MAINWINDOW_H

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
MainWindow w;
ui->pushButton->setText("Ferma riconoscimento");
w.cattura();
}


void MainWindow::cattura()
{
cameraTimer.start(33); // 33 ms = 30 fps
connect(&cameraTimer, SIGNAL(timeout()),this,SLOT(cameraTimerTimeout())) ;

return;

}

void MainWindow::cameraTimerTimeout()
{
std::cout << "CIAO2" << std::endl;

}



Thank you in advance!

ChrisW67
3rd March 2013, 22:32
Line 18 of mainwindow.cpp makes no sense. You are creating another instance of the MainWindow object and calling cattura() on that (line 20) before immediately discarding the instance (line 21).

lifedj
3rd March 2013, 22:43
I know, but it is a simplified version of the code! In the function cattura I have to do other things! ;-)

ChrisW67
3rd March 2013, 22:53
It is a simplified and broken piece of code that doesn't work how you expect because you are throwing away the object you are expecting to do the work. The bug is in on_pushButton_clicked() and has nothing to do with cattura().

lifedj
3rd March 2013, 23:12
If it is so I didn't understand what you said in the first answer!
Can you explain with more details what is the problem and how can I solve it?

I thought that you were saying me that I call the function "cattura" but in it I do only 2 things! Excuse me for the misunderstanding!

Added after 4 minutes:

I have just understood!
I create an instance of something that is already istanced! I deleted only the line 18 and now it works fine (with substition of "cattura();" with "cattura();"!

Excuse me and thank you! ;-)

ChrisW67
3rd March 2013, 23:49
You're welcome... everybody has done something similar at one time or another.

d_stranz
4th March 2013, 20:20
void MainWindow::cattura()
{
cameraTimer.start(33); // 33 ms = 30 fps
connect(&cameraTimer, SIGNAL(timeout()),this,SLOT(cameraTimerTimeout())) ;

return;

}

Since "cameraTimer" is a member variable of MainWindow, this connect() statement should be made in the MainWindow constructor, not in this slot.

What will happen in the code above is that each time you push the capture button, you will make *another* connection to the camera timeout signal. So, the first time you push the button, the slot will be called once on timeout. The second time, it will be called twice, the third time it will be called three times, and so on. By the time you get to 10 pushes, you'll be saying "CIAO" so many times you will wish it really was leaving.