Results 1 to 10 of 10

Thread: QTimer problem

  1. #1
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4

    Default QTimer problem

    Hello
    I am trying to insert qtimer in my program but in run time when I enter a function
    timer does not go into the slot. I want qtimer to work parallel with the other code in my program. Here is the code:
    -----------------------------------------
    testplug::testplug(QWidget *parent, Qt::WFlags flags)
    : QWidget(parent, flags)
    {
    ui.setupUi(this);
    a = 0;
    connect(ui.pushButton,SIGNAL(clicked()),this,SLOT( myclick()));
    connect(&mytimer,SIGNAL(timeout()),this,SLOT(denem e()));
    mytimer.start(1000);
    }

    void testplug::deneme()
    {
    printf("Var is = %d\n",a++);

    }
    void testplug::myclick()
    {
    ::Sleep(3000);
    printf("exiting..");

    }
    ---------------------------------
    In the above code when it is in the myclick function, timeout occurs but it does not goes into the deneme() function.

    What can be the problem ?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer problem

    The timer will work only if the event loop works, but you block it with sleep().

  3. #3
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4

    Default Re: QTimer problem

    I am removing sleep() and making something in the function.
    In this case it is not runs the deneme() when the timeout occurs.

  4. #4
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4

    Default Re: QTimer problem

    In Qt when an ordinary function is called, does event loop run?
    I think it runs.
    I change the myclick()

    void myclick()
    {
    for(int i = 0;i<5000;++i);

    }
    and again QTimer does not work when the myclick() runs.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer problem

    Because you're still blocking the loop. Connect a timeout() signal of the timer to a custom slot, start the timer and return from the myclick() slot. Your other slot will be called when the timer timeouts.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer problem

    Quote Originally Posted by vv View Post
    In Qt when an ordinary function is called, does event loop run?
    No, because you have a single threaded application. Only one thing can happen at the same time.

    Quote Originally Posted by vv View Post
    void myclick()
    {
    for(int i = 0;i<5000;++i);

    }
    and again QTimer does not work when the myclick() runs.
    It doesn't differ much from the one with sleep(). If you want to write such long loops, you have to invoke QCoreApplication::processEvents() from time to time to process events from the event queue.

  7. #7
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4

    Default Re: QTimer problem

    Thank you very much Jacek for your replies..

  8. #8
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4

    Default Re: QTimer problem

    Hi again,

    I have a problem again.
    I set my timer interval 1000 msec. In the myclick() slot sleep(5000).
    After sleep, I call the processEvents() but it only once runs the timer slot deneme().
    But it must run the deneme at least four times I think. Am I right?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer problem

    No, you're wrong. It should fire only once. Thus you should avoid using sleep in your application I really suggest you get rid of the busy loop and use a separate slot.

  10. #10
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4

    Default Re: QTimer problem

    ok wysota.. I understan at last thanks

Similar Threads

  1. Replies: 5
    Last Post: 6th March 2007, 05:34
  2. Problem combining QWorkspace & QGLWidget
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2006, 21:45
  3. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  4. Qthread n QTimer Problem
    By quickNitin in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2006, 14:12
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.