TGUI Forum

General Category => Help requests => Topic started by: Kvaz1r on 26 August 2019, 10:18:07

Title: Positioning of widgets inside Grid.
Post by: Kvaz1r on 26 August 2019, 10:18:07
Is it possible to set position for widgets inside grid to keep it straight(i.e. on one row/column)?
When there is only one column better don't use grid at all, but it should be possible for multiple columns.


Code: [Select]
#include <TGUI/TGUI.hpp>
#include <random>
#include <string>

std::string random_str()
std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 32);
std::shuffle(str.begin(), str.end(), gen);
return str.substr(0, dis(gen));

int main()
sf::RenderWindow window(sf::VideoMode(800, 600), "TGUI window");
tgui::Gui gui(window);
auto panel = tgui::ScrollablePanel::create();
auto grid = tgui::Grid::create();

for (std::size_t i = 0; i < 20; i++)
auto cbox = tgui::Label::create(random_str());
//panel->add(cbox, std::to_string(i));
grid->addWidget(cbox, i, 0);
if (i != 0)
auto prev = grid->getWidget(i - 1, 0);
//auto prev = panel->get(std::to_string(i - 1));//works without grid
if (prev != nullptr)
cbox->setPosition({ tgui::bindLeft(prev), tgui::bindBottom(prev) });

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

gui.handleEvent(event); // Pass the event to the widgets

gui.draw(); // Draw all widgets

P.S. I read this topic ( but it's didn't help  :)
Title: Re: Positioning of widgets inside Grid.
Post by: texus on 26 August 2019, 10:30:14
If I understand it correctly then you are just trying to put the labels on the left size of the cell? If that is the case then you only need to pass 2 more parameters to addWidget (the first one being a padding, the second one being the alignment).
Code: (cpp) [Select]
grid->addWidget(cbox, i, 0, {0}, tgui::Grid::Alignment::Left);
Title: Re: Positioning of widgets inside Grid.
Post by: Kvaz1r on 26 August 2019, 11:15:24
right, it's indeed works, thank you.