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. QThread Advice
Forum Updated to NodeBB v4.3 + New Features

QThread Advice

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 299 Views 1 Watching
  • 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.
  • R Offline
    R Offline
    RCtaf
    wrote on last edited by RCtaf
    #1

    Hello Guys!

    So i am doing this big project right now and i need a advice or 2 concerning QThreads.
    How do i start a second thread in my mainwindow ? cause everytime i try that, my program crashes.
    the solution i found working is the one below. Basically i am starting-waiting-quitting my thread function from main.
    See the code below:

    #include <QApplication>
    #include "mainwindow.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc,argv);
    
        MainWindow myMainWindow;
        myMainWindow.startJoyThread();
        myMainWindow.show();
        return  app.exec();
        myMainWindow.quitJoyThread();
        myMainWindow.waitOnJoyThread();
    }
    
    

    with startJoyThread(), quitJoyThread() and waitOnJoyThread() looking like this.

    void MainWindow::startJoyThread()
    {
        m_JoystickInputThread->start();
    }
    
    void MainWindow::waitOnJoyThread()
    {
        m_JoystickInputThread->wait();
    }
    
    void MainWindow::quitJoyThread()
    {
        m_JoystickInputThread->quit();
    }
    
    

    m_JoystickInputThread is the thread object created from the QThread class.

    my question is : is there a clean and better way i can do this ? i mean start my 2nd thread on my mainwindow without my whole program crashing? Any inputs will be welcomed.Thank you so much!

    JKSHJ 1 Reply Last reply
    0
    • R RCtaf

      Hello Guys!

      So i am doing this big project right now and i need a advice or 2 concerning QThreads.
      How do i start a second thread in my mainwindow ? cause everytime i try that, my program crashes.
      the solution i found working is the one below. Basically i am starting-waiting-quitting my thread function from main.
      See the code below:

      #include <QApplication>
      #include "mainwindow.h"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc,argv);
      
          MainWindow myMainWindow;
          myMainWindow.startJoyThread();
          myMainWindow.show();
          return  app.exec();
          myMainWindow.quitJoyThread();
          myMainWindow.waitOnJoyThread();
      }
      
      

      with startJoyThread(), quitJoyThread() and waitOnJoyThread() looking like this.

      void MainWindow::startJoyThread()
      {
          m_JoystickInputThread->start();
      }
      
      void MainWindow::waitOnJoyThread()
      {
          m_JoystickInputThread->wait();
      }
      
      void MainWindow::quitJoyThread()
      {
          m_JoystickInputThread->quit();
      }
      
      

      m_JoystickInputThread is the thread object created from the QThread class.

      my question is : is there a clean and better way i can do this ? i mean start my 2nd thread on my mainwindow without my whole program crashing? Any inputs will be welcomed.Thank you so much!

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @RCtaf said in QThread Advice:

      is there a clean and better way i can do this ?

      You can put quit() and wait() in your MainWindow's destructor, instead of calling them through main().

      start my 2nd thread on my mainwindow without my whole program crashing?

      You must make sure you understand the low-level details of threads if you want to use them. Improper thread use can cause a variety of bugs and headaches that are difficult to debug.

      Are you certain that you need a second thread in this case?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      3
      • R Offline
        R Offline
        RCtaf
        wrote on last edited by
        #3

        Thanks for the reply!

        That worked! i got rid of the quit() and wait() from my main.

        I am very new when its come to threads. unfortunately i need the second thread, because i am using a SDL (SDL_Joystick) class to continually handle inputs from a joystick and the best way i found so far was running it through a second thread.

        JKSHJ 1 Reply Last reply
        0
        • R RCtaf

          Thanks for the reply!

          That worked! i got rid of the quit() and wait() from my main.

          I am very new when its come to threads. unfortunately i need the second thread, because i am using a SDL (SDL_Joystick) class to continually handle inputs from a joystick and the best way i found so far was running it through a second thread.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @RCtaf said in QThread Advice:

          Thanks for the reply!

          That worked! i got rid of the quit() and wait() from my main.

          Great!

          I am very new when its come to threads

          You will benefit from reading and researching.

          unfortunately i need the second thread, because i am using a SDL (SDL_Joystick) class to continually handle inputs from a joystick and the best way i found so far was running it through a second thread.

          OK

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          1

          • Login

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