[SOLVED]Qt and SFML: Widget is crashing while Window is open
-
Hello... I am integrating Qt with SFML wherein, when a button on the QWidget is clicked, an SFML window should be opened. This works perfectly, I am clicking the button on the widget and the SFML window is being opened. However, after I click the button and the window starts running, the widget becomes unusable. If I try to click or type anything on the widget while the window is running, the application crashes. Here's the code of what I'm doing,
void Widget::on_Generate_Button_clicked() { Open_Window(); } void Widget::Open_Window() { sf::Window window(sf::VideoMode(800, 600), "My window"); while(window.isOpen()) { sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed) window.close(); } window.clear(); window.display(); } }
I don't know what's wrong exactly, but I think the window is kind of blocking the event loop or something. I can't get back the control of the widget until the window is closed. The window does not return control back to the widget until it is closed. How can I fix this in such a way that I can control the widget simultaneously even while the window is running?
I tried to ask this on the SFML forums, but they said this is something related to Qt event loop and couldn't really give a solution. They suggested using multithreading but said they don't recommend using it. I don't know how to do multithreading that much and I'm on a very tight schedule for the deadline to learn properly now. Is there an alternative solution? Please help. Thank you.
-
Hi,
Your double while loop blocks Qt's event processing. One thing you can try is to call
qApp->processEvents();
in it. -
@SGaist Hi SGaist,
Thank you VERY MUCH for replying! You have saved an entire life here! I tried your solution and IT WORKS LIKE A CHARM! I tested it extensively generating multiple windows and the widget is working perfectly fine. I upvoted your reply and I am marking this topic as solved. Thank you very much once again. I salute you, sir!