problem with Grid

Started by Djer, 16 June 2015, 15:59:19



First thanks you very much for this amazing library, it makes my life easier.

I want to create a map editor for a small game in a resizable window. So I think that Grid can help me.

For now I tried to create a MenuBar inside a Grid but after many tests, It doesn't work.

Here is a summary of my code :

    m_fileMenuBar->addMenuItem("File", "New");
    m_fileMenuBar->addMenuItem("File", "Open");
    m_fileMenuBar->addMenuItem("File", "Save");
    m_fileMenuBar->addMenuItem("File", "Save as");
    m_fileMenuBar->addMenuItem("File", "Close");
    m_fileMenuBar->addMenuItem("File", "Quit");

    m_fileMenuBar->addMenuItem("Edit", "Copy");
    m_fileMenuBar->addMenuItem("Edit", "Cut");
    m_fileMenuBar->addMenuItem("Edit", "Paste");


    m_fileMenuBar->setSize(800, 40);



    m_mainGrid->addWidget(m_fileMenuBar, 0, 0);
    m_mainGrid->addWidget(m_button1, 0, 1);
    m_mainGrid->addWidget(m_button2, 0, 2);
    m_mainGrid->setSize(1920, 1024);

(That code is for testing)

The two Button don't display their text and the Menubar doesn't respond to mouse clic correctly.

Do I do anything incorrectly ?

--> Sorry for my English, I speak French ... :)


MenuBar doesn't really belong in a Grid. I've never tested it but it might give technical problems since it uses the width of its parent by default (which normally is the gui, but in this case the Grid which might not have a width yet). So the problem with MenuBar not being clickable might be a consequence of such a problem.

The buttons not having text can be caused by multiple things. It is basically because they are not getting the font correctly from their parent (the grid).

But in order to fix these problems I would need to see the whole code, not just this part. Could you write some minimal code that I can just copy-paste and run here?


Tanks for your fast response ! :)

Do you have an idea of how I should do to have a beautiful resizable program with no Grid?

I just want the program are not made only for one fixed resolution ... :/

And for the buttons who doesn't display their text here is my minimal code :

#include <TGUI/TGUI.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
    sf::RenderWindow window(sf::VideoMode(1024, 768), "My window");

    tgui::Gui gui(window);

        std::cout << "Erreur lors du chargement de la police" << std::endl;

    tgui::Grid::Ptr grid(gui);
    tgui::Button::Ptr buttonA(*grid);
    tgui::Button::Ptr buttonB(*grid);



    grid->addWidget(buttonA, 0, 0);
    grid->addWidget(buttonB, 1, 0);
    grid->setSize(1024, 768);

    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)





The menu bar issue seems to come from the fact that the "Options" has no menu items. It should be solved when adding items to it.

The problem with the buttons is the order of initialization. You cannot change them before they are loaded. The load function has to be called before you call setText.

QuoteDo you have an idea of how I should do to have a beautiful resizable program with no Grid?
In v0.6 there is no build-in way, but Grid is not the only option. For more control you can basically have a function that you call every time the window is resized that will call setPosition and setSize on all your widgets. Obviously this can result in a lot of code but then you at least control where everything is positioned under any resolution.

Another option to consider is trying out tgui 0.7-dev. In this development version it is possible to give widgets relative positions and sizes (e.g. a button with a width of 80% of the window). The position and size will automatically be updated once the bound widget changes. The big downside of using v0.7-dev is that the version is not finished so updates will break the API and you will have to change some code (e.g. the way widgets are loaded is going to be completely rewritten before 0.7.0 is released).


Thank you very much.

I completely missed the order to call the functions for the buttons. It's more logic to first load it effectively ...

And for my program I'll do like you said, I'll create a function who manage the widgets positions.

Thanks yet for your help ! :)