I have this struct
struct ViewData {
int ID;
std::string Name;
std::string Mode;
std::string Status;
std::string Type;
};
For some reason this function is not working at all, why? I will receive 3 sets of data in that struct generally. Ideally it would display all the labels in a series. But it is not drawing. Not even throwing an error or assertion warning.
for (std::size_t i = 0; i < parsedData.size(); ++i) {
const ViewData& data = parsedData[i];
// Create widgets for ID, Name, Mode, Status, and Type
std::cout << "\nProcessing data ID: " << data.ID << std::endl;
Gtk::Label idLabelWidget, nameLabelWidget, modeLabelWidget, statusLabelWidget, typeLabelWidget;
Gtk::Label idValueLabelWidget, nameValueLabelWidget, modeValueLabelWidget, statusValueLabelWidget, typeValueLabelWidget;
cout << "Label Creation at: " << data.ID;
idLabelWidget.set_text("ID:");
nameLabelWidget.set_text("Name:");
modeLabelWidget.set_text("Mode:");
statusLabelWidget.set_text("Status:");
typeLabelWidget.set_text("Type:");
cout << "Label Names at: " << data.ID;
idValueLabelWidget.set_label(std::to_string(data.ID));
nameValueLabelWidget.set_label(data.Name);
modeValueLabelWidget.set_label(data.Mode);
statusValueLabelWidget.set_label(data.Status);
typeValueLabelWidget.set_label(data.Type);
cout << "Label Values at: " << data.ID;
// Add labels to the grid
grid.attach(idLabelWidget, 0, row, 1, 1);
grid.attach(idValueLabelWidget, 1, row, 1, 1);
idLabelWidget.show();
idValueLabelWidget.show();
cout << "ID SET at: " << data.ID;
grid.attach(nameLabelWidget, 2, row, 1, 1);
grid.attach(nameValueLabelWidget, 3, row, 1, 1);
cout << "NAME SET at: " << data.ID;
grid.attach(modeLabelWidget, 4, row, 1, 1);
grid.attach(modeValueLabelWidget, 5, row, 1, 1);
cout << "MODE SET at: " << data.ID;
grid.attach(statusLabelWidget, 6, row, 1, 1);
grid.attach(statusValueLabelWidget, 7, row, 1, 1);
cout << "STATUS SET at: " << data.ID;
grid.attach(typeLabelWidget, 8, row, 1, 1);
grid.attach(typeValueLabelWidget, 9, row, 1, 1);
cout << "TYPE SET at: " << data.ID << endl;
//box.pack_start(grid, Gtk::PACK_SHRINK); // Add grid to box
cout << "************************************ at: " << data.ID << endl << std::flush;
row += 2; // Increment row for next set of widgets
}
Tried Gtk::Label
outside the loop. One of them printed, the second out of 3 elements. Then it threw assertion error for grid.attach
. Tried pushing the row down multiple lines. Same error.
From the code you have show, I suspect your problem to be related to scope. Here is a simplified version of your code:
for (std::size_t i = 0; i < parsedData.size(); ++i)
{
// Creating:
Gtk::Label idLabelWidget, nameLabelWidget, modeLabelWidget, statusLabelWidget, typeLabelWidget;
Gtk::Label idValueLabelWidget, nameValueLabelWidget, modeValueLabelWidget, statusValueLabelWidget, typeValueLabelWidget;
// Attaching:
grid.attach(idLabelWidget, 0, row, 1, 1);
grid.attach(idValueLabelWidget, 1, row, 1, 1);
grid.attach(nameLabelWidget, 2, row, 1, 1);
grid.attach(nameValueLabelWidget, 3, row, 1, 1);
grid.attach(modeLabelWidget, 4, row, 1, 1);
grid.attach(modeValueLabelWidget, 5, row, 1, 1);
grid.attach(statusLabelWidget, 6, row, 1, 1);
grid.attach(statusValueLabelWidget, 7, row, 1, 1);
grid.attach(typeLabelWidget, 8, row, 1, 1);
grid.attach(typeValueLabelWidget, 9, row, 1, 1);
} // End of scope: labels destroyed.
On each iteration of the loop, the labels your declared inside de scope are destroyed. The Gtk::Grid::attach
call takes a reference to the widget that it attaches. This means you are left with a dangling reference.
You should try moving the label creation outside of the loop.