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. [SOLVED]Qt and SFML: Widget is crashing while Window is open

[SOLVED]Qt and SFML: Widget is crashing while Window is open

Scheduled Pinned Locked Moved General and Desktop
sfmlqwidget
4 Posts 2 Posters 2.3k 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.
  • W Offline
    W Offline
    WDR_937
    wrote on 8 Apr 2015, 12:26 last edited by WDR_937 4 Aug 2015, 22:44
    #1

    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.

    1 Reply Last reply
    0
    • W Offline
      W Offline
      WDR_937
      wrote on 8 Apr 2015, 21:54 last edited by
      #2

      Please help, guys! Any inputs are appreciated, good or bad (I don't mind being scolded as long as it gets the job done). :-)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 8 Apr 2015, 21:59 last edited by
        #3

        Hi,

        Your double while loop blocks Qt's event processing. One thing you can try is to call qApp->processEvents(); in it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        W 1 Reply Last reply 8 Apr 2015, 22:43
        1
        • S SGaist
          8 Apr 2015, 21:59

          Hi,

          Your double while loop blocks Qt's event processing. One thing you can try is to call qApp->processEvents(); in it.

          W Offline
          W Offline
          WDR_937
          wrote on 8 Apr 2015, 22:43 last edited by
          #4

          @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!

          1 Reply Last reply
          0

          1/4

          8 Apr 2015, 12:26

          • Login

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