Max
Max

Reputation: 2162

QWidget for Image

I am trying to create a simple QGridLayout of images in PyQt5 but have found myself stuck because QGridLayout only has functionality for adding widgets and layouts via .addWidget() and .addLayout().

Below is my code that expects a QWidget but is having problems because I can't seem to find any QWidgets that can handle a png/jpg file.

class myGrid(QWidget):
    def __init__(self):
        QObject.__init__(self)

        # Create Layout
        self.lay = QGridLayout()
        self.setLayout(self.lay)

        i1 = QIcon("imgs/img1.png")
        i2 = QIcon("imgs/img2.png")
        i3 = QIcon("imgs/img3.png")
        i4 = QIcon("imgs/img4.png")

        # Add Images to Layout
        self.lay.addWidget(i1, 0, 0)    # Error because type(i1) != QWidget

The above is just the first of the images that I would like to add to my layout. The only problem is that because QIcon is not technically a QWidget I receive a compiler error.

I looked at other QObjects that handle images, including QImage and QPainter but because these classes are all of type QtGui I can't pass them into my grid.

Any suggestions for how to produce a QWidget from an image or using a specific QWidget that handle's images?

Upvotes: 0

Views: 2323

Answers (1)

odelande
odelande

Reputation: 311

QLabel is the widget you need. It can display a QPixmap, not only text.

Upvotes: 5

Related Questions