How to make a variable-column grid using PyQt [clo

2019-09-10 23:08发布

问题:

I'm trying to display an array of image thumbnails using Python and Qt4. My problem is that I don't want to calculate the amount of columns for the grid, so that when the application is resized or my thumbnails get bigger, the number of columns automatically change. Actually I want to use Qlabel, because images are going to have file names and possibly buttons. Is there an easy way to do it?

Something like that:

回答1:

Brendan Abel's answer is the right and elegant way to use the power of Qt. However, if you find model-view architecture too heavy, I'd suggest you to use FlowLayout demonstrated in here.

Its rather quite easy to implement and may suit your needs.



回答2:

You should look into using a QGraphicsView. It's a good building block for truly custom widgets that don't really resemble any of the built-in widgets. It uses a model/view architecture and allows you pretty much unlimited flexibility how and where each item is drawn, as opposed to relying on the more limited QLayout system of placement.