Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThreads freeze main aplpications while running

QThreads freeze main aplpications while running

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 177 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gaetano03
    wrote on last edited by
    #1

    Hello,

    I am trying to use QThreads now with the worker object approach.

    In my main window class here is what I do

    pThread = new controllerThread(pc);      
    awgThread = new controllerThread(awgc);
    

    where controllerThread is a simple extension of QThread class which overrides the run since I don't want the run to call the exec (don't want to execute a loop of events, just call a DOWork function in the threads that when is finished terminates the thread)

    I am connecting then the two threads to the doWork function of pc and awgc through the following commands

    connect(cThread,SIGNAL(started()),pc,SLOT(run_test()));
    connect(cThread,SIGNAL(started()),awgc,SLOT(run_test()));
    

    where run test is the dowork function that I want to execute on the thread.

    I am then connecting a pushbutton to the start of the thread where I am starting the two threads with the following lines

        pc->moveToThread(pThread);
        pThread->start();
    
        awgc->moveToThread(awgThread);
        awgThread->start();
    

    the two threads are started correclty (I have a simple for loop that prints a counter), but the main window is completely freezed until the threads are terminated.

    What am I missing.

    Any help will be much appreciated.

    Thank you!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gaetano03
      wrote on last edited by
      #2

      OK seems I solved, as I was declaring pc and awgc as parent class of the main window (not sure how the threads were running at this point since it should not have been moving the threads to pThread and awgThread at all).

      But my point is now, if I declare pc and awgc in mainwindow with no parent class, how is the destruction of these objects handled when the main application is closed?

      THank you

      Christian EhrlicherC 1 Reply Last reply
      0
      • G Gaetano03

        OK seems I solved, as I was declaring pc and awgc as parent class of the main window (not sure how the threads were running at this point since it should not have been moving the threads to pThread and awgThread at all).

        But my point is now, if I declare pc and awgc in mainwindow with no parent class, how is the destruction of these objects handled when the main application is closed?

        THank you

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #3

        @Gaetano03 said in QThreads freeze main aplpications while running:

        how is the destruction of these objects handled when the main application is closed?

        Then properly quit the thread in the mainwindow's dtor, wait for finish and connect the finished slot to deleteLater as explained in the documentation.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved