0

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.

5
  • 3
    Please edit your question to provide a minimal reproducible example. As it stands currently there is not enough information to allow us to reproduce the problem. Having said that, have you tried calling item->show() for each item? Please also read about layouts. Commented Oct 29, 2024 at 13:53
  • 3
    Assuming you use that parent in the constructor for the super class, what you describe is explicitly written in the 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. Commented Oct 29, 2024 at 14:30
  • The solution you seek is what @G.M. already mentioned. Repaint does not call show(), nor does update(). On a different note, you should call update() and not repaint(). A good video about this can be found here: youtu.be/jCNxm1drQ8w?si=_xftvZoU9jPxEP7e Commented Oct 29, 2024 at 15:52
  • Other than what Amparo wrote above, you generally don't need to call update() at all for standard widgets (and certainly not repaint(), unless you know what you're doing and why), unless you have a widget subclass with a custom paintEvent() override and its drawn contents have changed, or you implement a subclass that explicitly needs manual update scheduling. Calling update() or repaint() on a not [yet] visible widget is obviously pointless: if it's hidden, there's nothing to paint. Commented Oct 29, 2024 at 20:23
  • Unrelated: You really should stop using the foreach macro. C++11 has range-for built in. Commented Oct 30, 2024 at 7:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.