Place widget in center of QScrollArea

2019-02-25 04:30发布

问题:

How can I place custom widget with fixed size in center of QScrollArea?

I can place the widget manually inside QScrollArea with myWidget.pos(x,y) but there is no sizeChanged signal in QScrollArea so this position will be wrong if scroll area changed.

回答1:

I used @thuga's suggestion to set the alignment, but additionally I had to set a fixed size for the widget (I was settings only the minimum size before that), and also I needed to use a horizontal layout instead of a vertical one. For some reason, the vertical layout disregards the scroll area's horizontal alignment.

I used setFixedSize() to set a fixed size for my custom widget, but the layout and the alignment can be set from Qt Designer.