2014-12-17 4 views
1

나는이 같은 이미지를 표시하는 창을 가지고 :픽스맵의 최대 너비와 높이를 설정하는 방법은 무엇입니까?

import sys 
from PyQt4 import QtGui 

class Window(QtGui.QWidget): 

    def __init__(self): 
     super(Window, self).__init__() 

     self.initUI() 

    def initUI(self): 

     pixmap = QtGui.QPixmap("image.jpg") 

     pixmapShow = QtGui.QLabel(self) 
     pixmapShow.setPixmap(pixmap) 

     grid = QtGui.QGridLayout() 
     grid.setSpacing(10) 

     grid.addWidget(pixmapShow, 0, 1) 

     self.setGeometry(400, 400, 400, 400) 
     self.setWindowTitle('Review')  
     self.show() 

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    ex = Window() 
    sys.exit(app.exec_()) 

가 어떻게이 픽스맵을 표시 허용 최대 폭과 최대 높이를 설정할 수 있습니까?

  • 이미지의 너비가 350 픽셀보다 크거나 200 픽셀보다 큰 경우 가로 세로 비율을 유지하면서 한 치수가 350 픽셀보다 작아 질 때까지 이미지의 크기를 줄여야합니다.
  • 이미지가 350x200보다 작 으면 크기 조정이 발생하지 않습니다.

답변

2

크기 조정 이벤트에 따라 Pixmap을 자동으로 조정하는 자신의 Pixmap 컨테이너 클래스를 정의 할 수 있습니다.

def initUI(self): 

    pixmapShow = PixmapContainer("image.jpg") 
    pixmapShow.setMaximumSize(350, 200) 

    grid = QtGui.QGridLayout(self) 
    grid.setSpacing(10) 
    grid.addWidget(pixmapShow, 0, 1) 

    self.setGeometry(400, 400, 400, 400) 
    self.setWindowTitle('Review')  
    self.show() 
: 코드에서 다음
class PixmapContainer(QtGui.QLabel): 
    def __init__(self, pixmap, parent=None): 
     super(PixmapContainer, self).__init__(parent) 
     self._pixmap = QtGui.QPixmap(pixmap) 
     self.setMinimumSize(1, 1) # needed to be able to scale down the image 

    def resizeEvent(self, event): 
     w = min(self.width(), self._pixmap.width()) 
     h = min(self.height(), self._pixmap.height()) 
     self.setPixmap(self._pixmap.scaled(w, h, QtCore.Qt.KeepAspectRatio)) 

, 그것은 꽤 straigthforward입니다