Solved QThread Advice
-
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!
-
@RCtaf said in QThread Advice:
is there a clean and better way i can do this ?
You can put
quit()
andwait()
in your MainWindow's destructor, instead of calling them throughmain()
.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?
-
Thanks for the reply!
That worked! i got rid of the
quit()
andwait()
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.
-
@RCtaf said in QThread Advice:
Thanks for the reply!
That worked! i got rid of the
quit()
andwait()
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