Manage QEvent manual in different thread
-
I'm using glfw for gui, and use qt core library. Like this
int main() { QApplication app; // I also use QPainter for image and text. So i need QApplication in main thread std::thread([](){ while(1) { //render something with glfw } }).death(); app.exec(); }now i want to use qevent for render thread. Like this
int main() { QCoreApplication app; std::thread([](){ while(1) { QCoreApplication::ProcessEvent(); //manage event manual before every render. //render something with glfw } }).death(); QTimer timer; // sent some event to glfw thread app.exec(); }Can my goal be achieved?
-
I'm using glfw for gui, and use qt core library. Like this
int main() { QApplication app; // I also use QPainter for image and text. So i need QApplication in main thread std::thread([](){ while(1) { //render something with glfw } }).death(); app.exec(); }now i want to use qevent for render thread. Like this
int main() { QCoreApplication app; std::thread([](){ while(1) { QCoreApplication::ProcessEvent(); //manage event manual before every render. //render something with glfw } }).death(); QTimer timer; // sent some event to glfw thread app.exec(); }Can my goal be achieved?
-
@jsulm ... and why use std::thread when one wants to use Qt signal/slot and other functionality in there?
-
int main() { QCoreApplication app; QTimer timer; // sent some event to glfw thread while(1) { QCoreApplication::ProcessEvent(); //manage event manual before every render. //render something with glfw } return 0; }Can't believe this work.
However, i send Event to top object, it's seem not send to child object.
I want to create a static object as all receiver father. So i just need send event to this static object and it handle to child.
Like thisQObject top_object; QObject r1(&top_object); QObject r2(&top_object); PostEvent(top_object, event); //so r1 deal event first, then r2. -
@Quiccz said in Manage QEvent manual in different thread:
I thought qt event loop must start with exec().
Yes, you have it already: app.exec()
So, still don't know why you need a second thread. -
@Quiccz said in Manage QEvent manual in different thread:
I thought qt event loop must start with exec().
Yes, you have it already: app.exec()
So, still don't know why you need a second thread. -
@jsulm because app.exec() will block, and i need render my frame in while(1) loop. if in same thread, app.exec() will never run.
I found QEvent can be send and recive without exec(). So i don't need a second thread. which i don't know before
@Quiccz said in Manage QEvent manual in different thread:
because app.exec() will block, a
app.exec() does not block anything. If you want to render something (to whereever) then do it inside the event loop e.g. with a QTimer or during the paintEvent() when you want to paint it on the screen.
-
@Quiccz said in Manage QEvent manual in different thread:
because app.exec() will block, a
app.exec() does not block anything. If you want to render something (to whereever) then do it inside the event loop e.g. with a QTimer or during the paintEvent() when you want to paint it on the screen.
@Christian-Ehrlicher I mean if i run this
app.exec(); //this won't run until app.quit() while(1) { //renderloop }Or
while(1) { //renderloop } //exec won't run until render loop stop. app.exec()Or use timer
void loopfun { while(1) { //render } } QSingalTimer ( 10, loopfun); app.exec();eventloop will be block, because my loopfun never end.
If need let them run both, thread must be used.
-
Again: you don't need such a loop. As @Christian-Ehrlicher already suggested: either use a QTimer to trigger rendering or render in paintEvent()...