zoukankan      html  css  js  c++  java
  • pixmap和label设置图片自适应大小

    在label中添加pixmap来显示图片时,当图片过大时图片显示不全。

    1.这时可以使用pixmap的scared()方法,来设置图片缩放。

    QPixmap QPixmap.scaled (self, int w, int h, Qt.AspectRatioMode aspectMode = Qt.IgnoreAspectRatio, Qt.TransformationMode mode = Qt.FastTransformation)

    Scales the pixmap to the given size, using the aspect ratio and transformation modes specified by aspectRatioMode and transformMode.

    • If aspectRatioMode is Qt.IgnoreAspectRatio, the pixmap is scaled to size.
    • If aspectRatioMode is Qt.KeepAspectRatio, the pixmap is scaled to a rectangle as large as possible inside size, preserving the aspect ratio.
    • If aspectRatioMode is Qt.KeepAspectRatioByExpanding, the pixmap is scaled to a rectangle as small as possible outside size, preserving the aspect ratio.

    If the given size is empty, this function returns a null pixmap.

    In some cases it can be more beneficial to draw the pixmap to a painter with a scale set rather than scaling the pixmap. This is the case when the painter is for instance based on OpenGL or when the scale factor changes rapidly.

    2.可以使用QLabel.setScaledContents (self, bool)方法来使pixmap自适应label大小

    测试代码:

    #encoding:utf-8
    '''
    Created on 2016年7月10日

    @author: Administrator
    '''
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    import sys

    class ImageFrame(QMainWindow):
    def __init__(self):
    super(ImageFrame, self).__init__()
    self.initUI()

    def initUI(self):
    #窗体设置
    self.setGeometry(100, 100, 500, 400)
    self.setMaximumSize(500, 400)
    self.setMinimumSize(500, 400)
    self.setVisible(True)
    self.statusBar()
    image = QAction(QIcon('open.png'), 'open', self)
    image.setShortcut('ctrl+o')
    image.setStatusTip('open new image')
    self.connect(image, SIGNAL('triggered()'), self.openImage)
    toolbar = self.addToolBar('image')
    toolbar.addAction(image)
    label = QLabel()
    label.setGeometry(0, 0, 400, 400)
    self.setCentralWidget(label)
    layout = QGridLayout()
    self.label1 = QLabel()
    self.label1.setGeometry(0, 0, 200, 200)
    #设置label对齐方式
    self.label1.setAlignment(Qt.AlignLeft)
    button = QPushButton('edit')
    edit = QLineEdit()
    layout.addWidget(self.label1, 0, 0)
    layout.addWidget(edit, 1, 0)
    layout.addWidget(button, 1, 1)
    label.setLayout(layout)

    self.updataImage()

    def openImage(self):
    imageName = QFileDialog.getOpenFileName(self,"Open file dialog","/","jpg files(*.jpg)")
    self.updataImage(imageName)

    def updataImage(self, imageName = 'icon.png'):
    pixmap = QPixmap(imageName)
    '''图像缩放:使用pixmap的scare方法,参数aspectRatioMode=Qt.KeepAspectRatio设置为等比例缩放,
    aspectRatioMode=Qt.IgnoreAspectRatio为不按比例缩放'''
    scaredPixmap = pixmap.scaled(400, 400, aspectRatioMode=Qt.KeepAspectRatio)
    #图像缩放:使用label的setScaledContents(True)方法,自适应label大小
    #self.label1.setScaledContents(True)
    print pixmap.height()
    print pixmap.width()
    self.label1.setPixmap(scaredPixmap)

  • 相关阅读:
    Kubenetes环境搭建笔记
    Python+Robot Framework实现UDS诊断自动化测试
    Python实现CAN总线J1939报文接收、发送
    [转载]从SQL 2008 复制数据及对像到SQL 2000 的方法
    推荐移动应用:群落(Groupcells)——全球第一款基于图片组的近场社交电子商务平台
    [缓存]迅雷下载原理
    HP大中华区总裁孙振耀退休感言
    [缓存]HTTP协议中的TranferEncoding:chunked编码解析
    [转载]SQL 2008到2005和2000版本的转换
    [学习]SVM入门(一)
  • 原文地址:https://www.cnblogs.com/daniaofighter/p/5713127.html
Copyright © 2011-2022 走看看