Positioning of widgets inside Grid.

  • 2 Replies
  • 38 Views
Positioning of widgets inside Grid.
« 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.

MCVE:

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

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

  • *****
  • 1463
    • View Profile
    • Texus's Blog
Re: Positioning of widgets inside Grid.
« Reply #1 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).
grid->addWidget(cbox, i, 0, {0}, tgui::Grid::Alignment::Left);

Re: Positioning of widgets inside Grid.
« Reply #2 on: 26 August 2019, 11:15:24 »
right, it's indeed works, thank you.