Results 1 to 14 of 14

Thread: use Qtimer in gui application with some thread at background

  1. #1
    Join Date
    May 2012
    Posts
    33
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default use Qtimer in gui application with some thread at background

    how can i use Qtimer in gui application with some thread at background?
    plz give me an example

  2. #2
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: use Qtimer in gui application with some thread at background

    What would you like to achieve in detail?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: use Qtimer in gui application with some thread at background

    how can i use Qtimer in gui application with some thread at background?
    This is generally bad idea, specially when its asked by a newbie.
    You probably don't need a thread.
    How to use a QTimer.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    May 2012
    Posts
    33
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use Qtimer in gui application with some thread at background

    well... let me explain the situsatuion... for example i have 100 links. im trying to download each of them with an Qhttp object..

    for(link1 to link100)
    {
    .
    .
    .
    QHttp myHttp = new QHttp(some host);
    myHttp.get(some path)
    }

    till here.. ok?

    as u know Qhttp inhertis from Qobject and also Qobject inherits from Qthread so, each instance of Qhttp is a thread by itself... thus the multithreading part is done... is it right?

    now i need to make some delay between my requests.. for example 5 seconde.... i add this code to the above code....


    for(link1 to link100)
    {
    .
    .
    .
    QHttp myHttp = new QHttp(some host);
    myHttp.get(some path)
    QTimer timer;
    connect(timer,SIGNAL(timeout()),this,SLOT(someslot ()))
    timer.start(5000);
    }

    but it does not work....

    is it clear? if not i can paste my code here.. thanks

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

    Default Re: use Qtimer in gui application with some thread at background

    QObject does not inherit QThread. QHttp is not a thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2012
    Posts
    33
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use Qtimer in gui application with some thread at background

    so u r right... i made a mistake ... QThread inherits Qobject...
    am i wrong? i mean each instance of QHttp isnt a thread by it self?

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: use Qtimer in gui application with some thread at background

    k.qasempour, the question of the foundations of C++ : what happens with timer at line 11 ?
    Qt Code:
    1. for(link1 to link100)
    2. {
    3. .
    4. .
    5. .
    6. QHttp myHttp = new QHttp(some host);
    7. myHttp.get(some path)
    8. QTimer timer;
    9. connect(timer,SIGNAL(timeout()),this,SLOT(someslot ()))
    10. timer.start(5000);
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Lesiok for this useful post:

    k.qasempour (11th July 2012)

  9. #8
    Join Date
    May 2012
    Posts
    33
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use Qtimer in gui application with some thread at background

    i know what u mean... but now my question is
    each instance of QHttp isnt a thread by it self?

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: use Qtimer in gui application with some thread at background

    each instance of QHttp isnt a thread by it self?
    You tell us!
    QHttp
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: use Qtimer in gui application with some thread at background

    Quote Originally Posted by k.qasempour View Post
    i mean each instance of QHttp isnt a thread by it self?
    Read the docs, please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: use Qtimer in gui application with some thread at background

    As far as it is concerned with this particular post, I solve my own problem using single shots. let's say you need to execute a slot two seconds after:

    QTimer::singleshot(2000, this, SLOT(slotname()));

    I used singleshot inside most of my objects in a project. if you still have problems with it, I can check it out for further details.

    and, singleshot or your QTimer.start do not delay the execution of the instruction below them. each timers as soon as called, will be run as a seperate thread. it's like a parent process creating a child of his own using fork() in c.

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

    Default Re: use Qtimer in gui application with some thread at background

    Quote Originally Posted by saman_artorious View Post
    each timers as soon as called, will be run as a seperate thread.
    No, that's false.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    May 2012
    Posts
    33
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use Qtimer in gui application with some thread at background

    it does not work again....
    here is my code... whats the problem?

    QTimer::singleShot(2000, this, SLOT(fake()));


    void MainWindow::fake()
    {
    int a;
    a++;

    }

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

    Default Re: use Qtimer in gui application with some thread at background

    This code does not do anything so how do you know it doesn't work?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTimer in a worker thread
    By inger in forum Newbie
    Replies: 6
    Last Post: 2nd November 2012, 12:37
  2. qtimer in console application
    By JeanC in forum Newbie
    Replies: 10
    Last Post: 31st March 2011, 15:30
  3. QTimer doesnt work or complains about thread
    By daemonna in forum Qt Programming
    Replies: 3
    Last Post: 1st September 2010, 16:00
  4. Replies: 9
    Last Post: 28th November 2009, 20:31
  5. QTimer - too many in application??
    By db in forum Newbie
    Replies: 4
    Last Post: 31st March 2008, 15:12

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
  •  
Qt is a trademark of The Qt Company.