List view

  • 5 Replies
  • 56 Views
List view
« on: 04 September 2019, 20:13:07 »
I want to create a list view and at this moment there are no tutorials covering TGUI and the question is: How I connect each individual items of a list view? For example I wrote this code:
void Editor::Open_dir(tgui::Gui& Editor_gui)
{
    tgui::ListView::Ptr file_listview = tgui::ListView::create();
    std::string path = "Maps";
    for(const std::filesystem::directory_entry& files : std::filesystem::directory_iterator(path))
    {
        file_listview->addItem(files.path().c_str());
        file_listview->connect("pressed", Open_file /*, ...*/);
    }    
}
 
or is this incorrect?

*

texus

  • *****
  • 1463
    • View Profile
    • Texus's Blog
Re: List view
« Reply #1 on: 04 September 2019, 20:17:26 »
The ListView has a "ItemSelected" signal that gets called when an item is highlighted and a "DoubleClicked" for when double clicking an item. If the Open_file function has an int parameter then it will contain the index of the selected item.

Re: List view
« Reply #2 on: 04 September 2019, 20:19:11 »
There is a documentation, so you can find there all available signals in section Public Attributes. 

Re: List view
« Reply #3 on: 04 September 2019, 20:22:38 »
So, what are you saying is that the connect function will just do its thing with the item that was just added?

Re: List view
« Reply #4 on: 04 September 2019, 20:43:30 »
Yes. Here simple example how you can get item that was clicked, my compiler still doesn't recognize that filesystem is part of std so sorry about that  :)

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

#include <string>
#include <iostream>
#include <filesystem>

int main()
{
sf::RenderWindow window(sf::VideoMode(400, 300), "TGUI window");
tgui::Gui gui(window);

auto panel = tgui::ScrollablePanel::create();
auto listView = tgui::ListView::create();
listView->setExpandLastColumn(true);
listView->connect("ItemSelected", [listView](int id) {
std::cout << "Click on: ";
for (const auto& el : listView->getItemRow(id))
{
std::cout << std::string(el) << ' ';
}
std::cout << "\n\n";
});

listView->setSize({ "100%,100%" });
listView->addColumn(L"   ID   ");
listView->addColumn(L"   File   ");

std::string path = ".";
auto i = 0;
for (const auto& entry : std::experimental::filesystem::directory_iterator(path))
{
listView->addItem({ std::to_string(++i), entry.path().generic_string() });
}
panel->add(listView);
gui.add(panel);

// Main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// When the window is closed, the application ends
if (event.type == sf::Event::Closed)
window.close();

// When the window is resized, the view is changed
else if (event.type == sf::Event::Resized)
{
window.setView(sf::View(sf::FloatRect(0.f, 0.f, static_cast<float>(event.size.width), static_cast<float>(event.size.height))));
gui.setView(window.getView());
}

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

window.clear();

// Draw all created widgets
gui.draw();

window.display();
}

return EXIT_SUCCESS;
}
« Last Edit: 04 September 2019, 20:46:20 by Kvaz1r »

Re: List view
« Reply #5 on: 04 September 2019, 22:08:25 »
You need to use std=c++17 flag in order to compile and thanks
« Last Edit: 04 September 2019, 22:10:36 by CatalinNic »