Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Qt and SFML Window  (Read 10352 times)

0 Members and 1 Guest are viewing this topic.

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Qt and SFML Window
« on: April 06, 2015, 01:18:58 am »
Hi... I am using SFML with Qt and I am creating a GUI where the button click will open an SFML window. Here's what I did. I created a function which opens an SFML window (the same generic SFML opening window example, except its not written in main but a separate function). Then, I made a Qt button click slot which, on clicking, calls this function. It runs perfectly. However, the button becomes frozen on clicked mode until the window is closed. And I can see why this is occuring, because the function does not return control back to the button until the window is closed. Furthermore, clicking on the button while the window is open causes the application to crash.

How would I fix this? How would I return control back to the button so that I can click on it while a window is open? Doing so, I would be able make the button create multiple windows in this way. I know this is possible because I've seen it being done with DirectX windows and Qt. I'm pretty sure this is not an error on part of SFML, but I thought I'd post here anyway because since it has been done with DirectX, it should be possible for SFML too. Please help. Thanks.

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Qt and SFML Window
« Reply #1 on: April 06, 2015, 08:11:20 am »
Have you crated a Qt Widget for SFML?
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Qt and SFML Window
« Reply #2 on: April 06, 2015, 08:49:20 am »
No... I didn't! Because I am not putting the SFML Window inside the Qt Window. I am separately creating a new window on the button click.

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Qt and SFML Window
« Reply #3 on: April 06, 2015, 10:51:18 pm »
We can only guess since you aren't providing any code so we can see what exactly you're doing.

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Qt and SFML Window
« Reply #4 on: April 07, 2015, 03:57:14 am »
The code is the same as the example of the tutorials for opening a window. And the code for the button click is created when I attach a slot to a GUI button. So, it's auto generated code. I'm just putting the tutorial code in a function instead of main so that the function is called whenever the button is clicked. If you still insist on seeing the code, then,

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();
        }
}

However, there is no point in me posting this since this is the same tutorial code copy pasted to my program copy pasted here. I'm pretty sure the error is not Qt or SFML specific. It's more about the general C++ concept of passing of control from a function to another function within its scope. I thought I'd post it here because, usually, the SFML window function does not return the control until the window is closed. Is there any way to bypass it?

Here's what I what:

At 3:58, the person opens one window and while this window is running, opens another at 4:46. He is able to use the button on the Qt Widget even though the window is running (He uses DirectX). That's what I want exactly. To have control of the button while the window is running. As of now, my program is not doing that, and crashing if I try to do that.

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Qt and SFML Window
« Reply #5 on: April 07, 2015, 06:03:01 am »
I don't have experience with Qt, but, do you have control of the main function? If so, I'd put the window code in it. All Open_Window should do is create the window. So it's going to need access to a sf::Window variable (by pointer, reference, or the sf::Window variable being a member of this Widget).

Something like:
int main(int argc, char** argv)
{
    // stuff
   
    while(window.isOpen())
    {
        // stuff
    }

    // stuff
}

void Widget::Open_Window()
{
    window.create({800, 600}, "My Window");
}
 

That way, the window will only be ran/updated/etc when it's open, while still letting everything else run normally. And the window should be recreated on every button press.

If you don't have control of main... I guess you could look into multi-threading, but I don't really recommend that.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Qt and SFML Window
« Reply #6 on: April 07, 2015, 09:37:13 am »
My guess is, that SFML is consuming all the events as long as the window is open, thus Qt doesn't get any events and don't register any clicks or any other action.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Qt and SFML Window
« Reply #7 on: April 07, 2015, 09:59:40 am »
It's not that SFML is consuming all the events, it's more than he's blocking the main thread, and so the Qt event loop cannot run anymore as long as the SFML window is open. You must never block like this in a Qt application (at least not in the main thread).
Laurent Gomila - SFML developer

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Qt and SFML Window
« Reply #8 on: April 07, 2015, 11:07:43 am »
OK... So, what do you reckon I should do? Should I follow the method suggested by dabbertorres? I don't know whether I have control of main. How would I know that? Should I run a printf statement in main while the window is running or something like that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Qt and SFML Window
« Reply #9 on: April 07, 2015, 12:29:14 pm »
Two solutions:

1. Run your SFML window in a thread.

2. Run the update (events, drawing) of your SFML window in function called by a Qt signal, so that it is inserted in the flow of the Qt event loop and doesn't break it.
Laurent Gomila - SFML developer

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Qt and SFML Window
« Reply #10 on: April 07, 2015, 04:35:06 pm »
OK... So,

1) I haven't really worked with threads before and I don't have time to start learning it now. Time is really of the essence here. Also, dabbertorres says he doesn't recommend using threads.

2) I can try this out, although I have no clue on what to do here. So far, I only used Qt signals for simple button clicks.

I did try out dabbertorres's solution and I got the white screen problem and subsequently the program crashed. I created the sf::Window Window variable on a global scope and called Window.create in the button click slot. And the update loop was placed in main. I am able to open multiple windows without the Widget GUI crashing, but conversely now the SFML window is crashing.  :-\

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Qt and SFML Window
« Reply #11 on: April 07, 2015, 04:44:52 pm »
The tutorial that was written for SFML 1.6 should show you the idea. Since it is outdated, just focus on the code that triggers the update of the SFML window, and nothing else ;)

http://www.sfml-dev.org/tutorials/1.6/graphics-qt.php

By the way, before trying such complicated things, you should really learn a little more about Qt and its core systems (signal / slots, meta-objects, event loop).
Laurent Gomila - SFML developer

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Qt and SFML Window
« Reply #12 on: April 07, 2015, 05:44:13 pm »
Thanks for that, Laurent. I'll try to work with that. I'm not sure how much I'll understand all that but I'm pretty sure I'll hit a snag somewhere, so I'm gonna post in this topic if I have any problem. Thanks for your help. You're the only API creator around the web that actively participates in interactions with the users. Kudos!  ;D

By the way, before trying such complicated things, you should really learn a little more about Qt and its core systems (signal / slots, meta-objects, event loop).

I know. But this is for a submission to a college admission and I'm really low on time. So I'm, um... let's use the word half-assedly... Yeah, I'm half-assedly learning only what is needed for the program in order to meet deadlines. So, if any anomaly occurs outside the scope of that, I become a lost sheep. But, yeah... Once this is over and I'm freelancing and such, I would definitely go deeper to learn more. Thanks for your advice.  :)

P. S. : I have observed most of the questions here (including some of mine) are asked by people who are too lazy to read the documentation. Most of them can be avoided and solved just by going through the documentation - something I learned later on when I was going through my earlier questions. Imagine that embarrassment.  :-\

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Qt and SFML Window
« Reply #13 on: April 09, 2015, 01:21:37 am »
UPDATE: I researched a little bit and found out that the window loop is blocking the Qt event processing. I was suggested a solution and it worked perfectly.

while(Window.isOpen())
{
        qApp->processEvents();
               
        while(Window.pollEvent(Window_Event))
        {
                //event loop
        }

        //rest of the stuff
}

Apparently, I have to call this function, qApp->processEvents(), in order to continue the Qt event processing even while the window is running. Easy solution, easy fix.  ;D

I am posting this here so that it might help any other lost sheep like me out there in future. Cheers.  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Qt and SFML Window
« Reply #14 on: April 09, 2015, 07:54:32 am »
This is really ugly, don't do that. Don't try to embed the Qt event loop in your SFML loop, do it the other way round, as shown in the 1.6 tutorial.

Stop listening to people who clearly know nothing about Qt, and start learning the right way of doing things :P
Laurent Gomila - SFML developer

 

anything