Positioning of widgets inside Grid.

Started by Kvaz1r, 26 August 2019, 10:18:07

Kvaz1r

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.

MCVE:

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

panel->add(grid);
gui.add(panel);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();

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

window.clear();
gui.draw(); // Draw all widgets
window.display();
}
}


P.S. I read this topic but it's didn't help  :)

texus

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

Kvaz1r