In the constructor I call the following method:
void UsersManagement::loadUsers()
{
QList<User> users = getUsers();
float y = 10;
foreach(User user, users)
{
UserWidget * item = new UserWidget (user.getID(), user.getUserName(), leftContainer);
item->setGeometry(20, y, width(), height());
y += item->height() + 10;
}
leftContainer->repaint();
}
leftContainer is a QWidget pointer. If I call this function in the constructor each UserWidget element is displayed. If I call after the window was loaded for the first time (to refresh the list) nothing changes.
I am not able to understand how to fix it.
item->show()for each item? Please also read about layouts.QWidget()constructor documentation: "If you add a child widget to an already visible widget you must explicitly show the child to make it visible". The only exception is when using layout managers (since they automatically show new children), which is something that should always be done to begin with, as setting arbitrary geometries is generally discouraged.update()at all for standard widgets (and certainly notrepaint(), unless you know what you're doing and why), unless you have a widget subclass with a custompaintEvent()override and its drawn contents have changed, or you implement a subclass that explicitly needs manual update scheduling. Callingupdate()orrepaint()on a not [yet] visible widget is obviously pointless: if it's hidden, there's nothing to paint.foreachmacro. C++11 has range-for built in.