Weird Rendering
-
I am trying to debug a rendering problem. At this moment, I have however no clue in which direction I should be looking and hence also have no idea yet how to isolate the problem and make a reproducable small example. The difficulty is also that it does not happend for all instances of the involved Widget class. Hopefully, somebody has a suggestion for me...
I have QGridLayout to which I add Widgets dynamically, i.e., by means of some function that I named repopulate(). This function is called upon creation of a Widget including a QScrollArea that includes the QGridLayout. This repopulate() function is called again when the content of the QGridLayout needs to be changed. In such case, the content of the QGridLayout is rebuild from scratch by first deleting all existing Widgets in the QGridLayout and subsequently repopulating the QGridLayout (I am aware that this is not necessarily the best approach, but it is the easiest to implement and I have done it in this way many times before without problems). The fact that the QGridLyout is used within a QScrollArea is potentially part of the problem. For some reason, the Widgets in the QGridLayout are not rendered correctly in some cases when repopulate() is called in the constructor of the Widget containing the QScrollArea that includes the QGridLayout. It is strange that it does not happen for all instances of the Widget.
Unfortunately, I cannot share a full screendump or actual code, but this is the undesired rendering effect. It shows that the size of the QLabels at the top line and the QLineEdits and other Widgets below the top line is compute incorrectly.

Any ideas in which direction I should look to identify what causes this weird rendering?
The code is Qt5-based and the problem appears on both Linux and Windows.
-
I am trying to debug a rendering problem. At this moment, I have however no clue in which direction I should be looking and hence also have no idea yet how to isolate the problem and make a reproducable small example. The difficulty is also that it does not happend for all instances of the involved Widget class. Hopefully, somebody has a suggestion for me...
I have QGridLayout to which I add Widgets dynamically, i.e., by means of some function that I named repopulate(). This function is called upon creation of a Widget including a QScrollArea that includes the QGridLayout. This repopulate() function is called again when the content of the QGridLayout needs to be changed. In such case, the content of the QGridLayout is rebuild from scratch by first deleting all existing Widgets in the QGridLayout and subsequently repopulating the QGridLayout (I am aware that this is not necessarily the best approach, but it is the easiest to implement and I have done it in this way many times before without problems). The fact that the QGridLyout is used within a QScrollArea is potentially part of the problem. For some reason, the Widgets in the QGridLayout are not rendered correctly in some cases when repopulate() is called in the constructor of the Widget containing the QScrollArea that includes the QGridLayout. It is strange that it does not happen for all instances of the Widget.
Unfortunately, I cannot share a full screendump or actual code, but this is the undesired rendering effect. It shows that the size of the QLabels at the top line and the QLineEdits and other Widgets below the top line is compute incorrectly.

Any ideas in which direction I should look to identify what causes this weird rendering?
The code is Qt5-based and the problem appears on both Linux and Windows.
@ModelTech said in Weird Rendering:
The fact that the QGridLyout is used within a QScrollArea is potentially part of the problem
That's a strange setup anyway. You usually don't want to add a layout to a
QScrollArea. You add a content widget, which should be scrolled instead.
I guess doing what you did, messes with the sizeHints and / or the widgets don't get the chance to expand.What happens if you show the same layout outside of the
QScrollArea? If it renders correctly, it's not a render issue, but, as I assumed above, a layout (or design) issue. Fix that and it should work. -
Thanks for your message. There is of course a Widget in between, otherwise it can't work at all. What I didn't do is subclass QScrollArea. But I will try to do so and see whether that solves the problem :)
@ModelTech said in Weird Rendering:
What I didn't do is subclass QScrollArea
Who said that this is a good idea?!
Are you able to display the content as desired if you resize the window?
Does it expand and give more space to theQLineEdit, in order to be rendered correctly -
Can you break this code?
Widget::Widget(QWidget *parent) : QWidget(parent) { QVBoxLayout *layout = new QVBoxLayout(this); QScrollArea *scrollarea = new QScrollArea(this); layout->addWidget(scrollarea); QWidget *content = new QWidget(scrollarea); content->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed); QGridLayout *grid = new QGridLayout(content); for (int col = 0; col < 10; ++col) { QLabel *label = new QLabel(QString("R0C%1").arg(col), content); grid->addWidget(label, 0, col); } for (int row = 1; row < 30; ++row) { for (int col = 0; col < 10; ++col) { QLineEdit *edit = new QLineEdit(content); edit->setText(QString("R%1C%2").arg(row).arg(col)); grid->addWidget(edit, row, col); } } scrollarea->setWidget(content); resize(800, 600); }