skip to Main Content
first_row = QtGui.QHBoxLayout()

setings_search = QtGui.QVBoxLayout()
search_label = QtGui.QLabel()
search_pixmap = QtGui.QPixmap()
search_pixmap.load('search.png')

#search_pixmap.scaledToWidth(130)
#search_pixmap.scaledToHeight(130)

search_label.setPixmap(search_pixmap)

setings_search.addWidget(search_label)
first_row.addLayout(setings_search)

The image doesn’t resize when I use both scaled and scaledToWidth/Heigth methods.
my window

On the picture I’ve shown what I already have and what I want to have.

Of course I can change size via Photoshop but I wonder how to do it programatically.

2

Answers


  1. Try defining paintEvent for your QWidget. However, I only know how to paint the pixmap on an absolute position (posX, poxY), not sure about relative position (i.e., in a QVBoxLayout).

    def paintEvent(self, event):
        qp = QtGui.QPainter()
        search_pixmap = QtGui.QPixmap('search.png')
        qp.drawPixmap(posX, posY, width, height, pixmap.scaled(width, height, transformMode=QtCore.Qt.SmoothTransformation))
    

    where width and height are dimensions of your scaled pixmap.

    Login or Signup to reply.
  2. As ekhumoro says in a comment, just use one of the scaled methods in QPixmap.

    from PyQt4 import QtGui
    
    app = QtGui.QApplication([])
    
    label = QtGui.QLabel()
    
    pixmap = QtGui.QPixmap()
    pixmap.load('test.png')
    pixmap = pixmap.scaledToWidth(20)
    label.setPixmap(pixmap)
    
    label.show()
    
    app.exec_()
    

    gives exactly the right size.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search