Results 1 to 5 of 5

Thread: Thread..:/

  1. #1
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Thread..:/

    I still make frist steps with Threads, however i know how they work.

    I have a strange situation:


    - MainWindow which based on (inherits) QMainWindow (this class contains method calculate(), and variable bool StartStop)

    Variable StartStop is steered by button (on/off).

    I want use calculate method when i click on a button.
    Unfortunately calculate method is not so short. So calculate() takes some time, and GUI of program does not respond.
    Is there any nice opportunity to put a thread which is sensitive for StartStop variable?
    Note that this is only one class. Moreover calculate() operates on some variables of MainWindow class.
    I was thinking of emitting signals. Any other ideas?
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Thread..:/

    Create a qobject based subclass that contains the signals and slots you want. Implement the complete calculation code inside this qobject subclass.

    In your main window, create an object based on this subclass (the calculation object).
    Connect all the signals and slots of the calculation object to your mainwindow object or other objects
    Then create a new empty qthread object.
    Move the calculation object to the new thread.
    Start the new thread.

    The on/off button can be connected to the thread start/stop slots or the calculation object itself. You have to see whatever makes more sense.

    Alternatively, you can use QtConcurrent.

  3. #3
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thread..:/

    That is pretty nice idea (however it took some for me to seperate blocks of code and organize them in other way).

    So now i have:

    -class MainWindow (contains Draw member)
    -class engine (resposible for some network data streaming and writing into files)
    -class Draw (which inherits engine and QObject) - resposible for drawing and contains QGraphicsScene member.

    Class Draw has SLOT: loop().

    Qt Code:
    1. void Draw::loop()
    2. {
    3. while(1){
    4. for(int a=0;a<200;a++){
    5. for(int b=0;b<8;b++)inttab[b]=b;calculate();}
    6. }
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow constructor contains this:



    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5.  
    6.  
    7. grafika=new Draw();
    8. connect(ui->actionStart,SIGNAL(triggered()),grafika,SLOT(loop())); // this line makes program terminated, why?
    9. this->moveToThread(&watek); //there is a member QThread watek
    10. //....etc.
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Molier; 5th December 2010 at 19:30.
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

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

    Default Re: Thread..:/

    You are moving a widget to a thread? That is bound to fail.
    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.


  5. #5
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thread..:/

    U r right in deed.
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

Similar Threads

  1. Replies: 9
    Last Post: 28th November 2009, 21:31
  2. Replies: 16
    Last Post: 7th October 2009, 09:17
  3. Replies: 6
    Last Post: 29th April 2009, 19:17
  4. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 23:09

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.