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

Author Topic: Any tricks to make a modal popup window stay on top of parent window ?  (Read 905 times)

0 Members and 1 Guest are viewing this topic.

physicskush

  • Newbie
  • *
  • Posts: 6
    • View Profile
I'm in the latter stages of my application and it is essential that I implement this feature - a modal window popup wherein the user must click a button to proceed with the parent window rendering. I need the pop-up window to remain on top and prevent activity (and focus) on the parent window.

In the following Minimally Reproducible Example, the child popup window correctly requests for focus but whenever one clicks on the parent window, the child window is no longer displayed.

I have searched the forums on how to make a RenderWindow always on top and it seems like it isn't an implemented feature, so I am asking, is there any workaround hacks to implement this manually ? I really need to have this feature otherwise my program is severely disfunctional.

#include <SFML/Graphics.hpp>
using namespace sf;

void drawPopUpWindow(RenderWindow& parentWindow) {
    RenderWindow popUpWindow(VideoMode(200,200), "Select a move", Style::Close);
    popUpWindow.setPosition(parentWindow.getPosition() + Vector2i(100,100));

    Event event;
    while (popUpWindow.isOpen()) {
        popUpWindow.clear(Color::White);

        while(popUpWindow.pollEvent(event)) {
            if(event.type == Event::Closed) popUpWindow.close();
            if(event.type == Event::LostFocus)
                if(parentWindow.hasFocus()) popUpWindow.requestFocus();
        }
        popUpWindow.display();
    }
}

int main() {
    RenderWindow window(VideoMode(500,500),"Parent");

    Event event;
    while (window.isOpen()) {
        window.clear(Color::White);

        window.pollEvent(event);        
        if(event.type == Event::Closed) window.close();
        if(event.type == Event::MouseButtonPressed)
            drawPopUpWindow(window);

        window.display();
    }
    return 0;
}
 

I understand that one solution is to spawn the child window next to the parent window, not on top of it, but I need my application to support full-screen mode.

Any suggestions on how one would implement this ? Thank you very much in advance.

 

anything