SFML community forums

Help => Window => Topic started by: WDR on April 06, 2015, 01:18:58 am

Title: Qt and SFML Window
Post by: WDR 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.
Title: Re: Qt and SFML Window
Post by: AlexxanderX on April 06, 2015, 08:11:20 am
Have you crated a Qt Widget for SFML?
Title: Re: Qt and SFML Window
Post by: WDR 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.
Title: Re: Qt and SFML Window
Post by: dabbertorres 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.
Title: Re: Qt and SFML Window
Post by: WDR 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: https://www.youtube.com/watch?v=CCxdZzLZsdg (https://www.youtube.com/watch?v=CCxdZzLZsdg)

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.
Title: Re: Qt and SFML Window
Post by: dabbertorres 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.
Title: Re: Qt and SFML Window
Post by: eXpl0it3r 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.
Title: Re: Qt and SFML Window
Post by: Laurent 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).
Title: Re: Qt and SFML Window
Post by: WDR 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?
Title: Re: Qt and SFML Window
Post by: Laurent 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.
Title: Re: Qt and SFML Window
Post by: WDR 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.  :-\
Title: Re: Qt and SFML Window
Post by: Laurent 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).
Title: Re: Qt and SFML Window
Post by: WDR 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.  :-\
Title: Re: Qt and SFML Window
Post by: WDR 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.  :)
Title: Re: Qt and SFML Window
Post by: Laurent 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
Title: Re: Qt and SFML Window
Post by: WDR on April 09, 2015, 04:45:51 pm
OH... MY... GOD...!

Fuck my life, man! Fuck my life! -_-
Title: Re: Qt and SFML Window
Post by: Jesper Juhl on April 09, 2015, 07:56:49 pm
OH... MY... GOD...!

Fuck my life, man! Fuck my life! -_-
Ehh?
How is that A) relevant. B) productive.  ?
Title: Re: Qt and SFML Window
Post by: WDR on April 09, 2015, 09:32:31 pm
Because my life is taking this prestigious opportunity to shit on me. I have 4 days left to submit a project for admission and I am on a very tight schedule. Every time I try to find a solution and I think I found it, something or the other puts me back at square one. So, one step forward, three steps back.

I am a slow learner, but I'm enthusiastic to learn. I got no one to teach me in real life and the internet is my only chance of learning. Problem is, people on the internet... Not really the kindest people in the world. I am not talking about people at SFML forums. You guys are great. A previous post in this topic even has me appreciating Laurent.

However, others... Specifically Stack Overflow (yeah, Stack Overflow, I said it)... not so kind. Sure, I don't understand right away. Sure, I'm a slow learner. But at least I'm asking politely. Nope! Just downvote the question and move on without giving any reason why. Is my question really not that worth answering? Sure, the answer to my question could be obvious and in plain sight but I'll still need to be pointed in the right direction. Google can only get you so far that you should know what exactly you're looking for.

Even then, I try not to ask more than what I should because I want to figure it out on my own and also, I don't want to be criticized for being ignorant. Even now, I'm scared to ask which part of the code is the update code that Laurent mentioned because I don't have a clue. But, I'm trying to figure it out on my own. You people are nice but I can't ask every single thing, even those unrelated to SFML. And the rest of the internet is... well, I told you. So, this is what I'm feeling right now.

Sorry for the long post, but I had to speak my mind. To answer your question, A) It's relevant as explained above and B) Is it productive if I don't say it? It doesn't make a difference.
Title: Re: Qt and SFML Window
Post by: Laurent on April 09, 2015, 10:33:28 pm
Well, if your solution works, and you've got 4 days left to finish your project, then I'd say go ahead and finish it, if all you need is that it runs. Whatever you do, you won't be able to solve your main problem in 4 days: learning the right SFML & Qt way to do this -- a rather complicated task for a beginner.

Quote
A previous post in this topic even has me appreciating Laurent.
:-\
Title: Re: Qt and SFML Window
Post by: WDR on April 09, 2015, 11:01:15 pm
Well, it works all right. Only thing is if I open multiple windows, I am only able to close the latest window first. Clicking on the 'x' close button on the older windows won't do anything but if I click on the latest window's close button, all of them close at once. But it's only minor and I guess it's OK. I don't think the admission committee will scrutinize it that deeply. I hope they don't. After all, I am going there to learn programming. This is just to test if I know the basics. Thanks anyway, Laurent.

Quote
A previous post in this topic even has me appreciating Laurent.
:-\

Hehe...  ;D
What was that supposed to mean? I was just trying to prove that I have to no hatred towards the SFML community. Is there some strict 'no-praise' policy I don't know about? If then, sorry for praising you.  :)
Title: Re: Qt and SFML Window
Post by: Laurent on April 10, 2015, 07:34:22 am
Quote
What was that supposed to mean?
What you said sounds like it's hard to appreciate me :P
Title: Re: Qt and SFML Window
Post by: WDR on April 11, 2015, 12:38:47 am
Well... That depends on the tone you infer it in. My implication was positive, though. You know that. Thanks anyway, Laurent.