Results 1 to 7 of 7

Thread: Problem with QTimer : the function that would be load is not load!

  1. #1
    Join Date
    Mar 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QTimer : the function that would be load is not load!

    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

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTimer>
    6. #include <iostream>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private slots:
    21. void on_pushButton_clicked();
    22. void cameraTimerTimeout();
    23. void cattura();
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27. QTimer cameraTimer;
    28. };
    29.  
    30. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::on_pushButton_clicked()
    17. {
    18. MainWindow w;
    19. ui->pushButton->setText("Ferma riconoscimento");
    20. w.cattura();
    21. }
    22.  
    23.  
    24. void MainWindow::cattura()
    25. {
    26. cameraTimer.start(33); // 33 ms = 30 fps
    27. connect(&cameraTimer, SIGNAL(timeout()),this,SLOT(cameraTimerTimeout()));
    28.  
    29. return;
    30.  
    31. }
    32.  
    33. void MainWindow::cameraTimerTimeout()
    34. {
    35. std::cout << "CIAO2" << std::endl;
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with QTimer : the function that would be load is not load!

    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).
    Last edited by ChrisW67; 3rd March 2013 at 22:50.

  3. #3
    Join Date
    Mar 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QTimer : the function that would be load is not load!

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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with QTimer : the function that would be load is not load!

    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().

  5. #5
    Join Date
    Mar 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QTimer : the function that would be load is not load!

    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! ;-)
    Last edited by lifedj; 3rd March 2013 at 23:12.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with QTimer : the function that would be load is not load!

    You're welcome... everybody has done something similar at one time or another.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QTimer : the function that would be load is not load!

    Qt Code:
    1. void MainWindow::cattura()
    2. {
    3. cameraTimer.start(33); // 33 ms = 30 fps
    4. connect(&cameraTimer, SIGNAL(timeout()),this,SLOT(cameraTimerTimeout()));
    5.  
    6. return;
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. QWebView load function
    By zgulser in forum Newbie
    Replies: 1
    Last Post: 16th August 2012, 19:52
  2. Problem when load JPG
    By HelderC in forum Newbie
    Replies: 10
    Last Post: 17th November 2010, 10:22
  3. Load Symbian DLL or use function from DLL in Qt App
    By newbis60 in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2010, 23:50
  4. SQL driver load problem
    By vieraci in forum Qt Programming
    Replies: 1
    Last Post: 12th December 2009, 14:59
  5. QWebView::Load problem
    By yogeshgokul in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2008, 10:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.