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)
pixmap和label设置图片自适应大小的更多相关文章
- css控制图片自适应大小
		
相信大家做网页时经常会碰到大分辨率的图片会把表格涨破以致漂亮的网页面目全非,但只要使用以下的CSS语句即可解决. 该CSS的功能是:大于600的图片自动调整为600显示. <style ...
 - iOS HTML 字符串中的图片 自适应大小
		
本文原文地址:http://www.cnblogs.com/qianLL/p/6095988.html 有时候 我们接收数据的时候 后台给的数据室一串HTML 的字符串 但是 我们要显示出来 这 ...
 - Android代码中动态设置图片的大小(自动缩放),位置
		
项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下: 1.布局文件 <RelativeLayout xmlns:android=&qu ...
 - Android drawableleft  drawableTop 设置图片的大小
		
例子: Drawable drawable=getResources().getDrawable(R.drawable.xx); //获取图片 drawable.setBounds(left, top ...
 - 设置图片自适应DIV大小
		
可以利用CSS样式表中表示后代的复合选择器进行设置.例: <head> <style type="text/css"> #right img /*设定box ...
 - android设置图片自适应控件大小
		
在XML文件的ImageView属性中加上:android:scaleType="fitXY"
 - word 2013 粘贴的图片自适应大小
		
1.先切换到页面视图 2.粘贴图片进去,成功自适应,像素不变,可右键图片另存为图片,查看原始图片,或者ctrl+滚轮上放大. 3.在其他视图就会出现超出范围的情况,还要自己调整
 - 纯js实现div内图片自适应大小
		
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - Winfrom PictureBox  设置图片自适应
		
初始状态 Bitmap bm = new Bitmap(Image.FromStream(System.Net.WebRequest.Create(new Uri(result.Result)).Ge ...
 
随机推荐
- React系列文章:Webpack模块组织关系
			
现代前端开发离不开打包工具,以Webpack为代表的打包工具已经成为日常开发必备之利器,拿React技术栈为例,我们ES6形式的源代码,需要经过Webpack和Babel处理,才能生成发布版文件,在浏 ...
 - 工具类TestTools
			
一些方法可能要使用到该工具类,该工具类中的方法包括从链接数据库到数据表中记录的增删改查. package JDBCTest; import java.io.InputStream; import ja ...
 - 什么是SASS
			
一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一 ...
 - C#基于RSA加密算法实现软件注册实战演练
			
一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理和 ...
 - Java全栈程序员之01:做个Linux下的程序猿
			
Windows10正在成为史上口碑最差的Windows系统 (图侵删) 我曾经花了数次1小时去寻找解决方案去关闭自动更新,包括停掉服务.修改注册表等等.但是都没有成功. 微软自身是知道这个问题的,但就 ...
 - [算法导论]插入排序 @ Python
			
class insertionsort(): def insertion_sort(self,Array): for i in range(1, len(Array)): key = Array[i] ...
 - js 模拟call、apply、bind实现
			
1.模拟call实现 Function.prototype.myCall = function (context) { var context = context || window // 给 con ...
 - 外网IP监测上报程序(使用Poco库的SMTPClientSession发送邮件)
			
目录 IPReport 项目介绍 编译说明 安装使用说明 获取外网IP方式 邮件发送关键代码 IPReport 代码地址https://gitee.com/solym/IPReport 项目介绍 外网 ...
 - Hadoop2.2.0分布式安装配置详解[3/3]
			
测试启动 按照下面的每一步执行,执行完一定要看输出的信息,注意warn或error或fatal的情况.因为这都是可能是问题出现的地方.出现一个问题,不解决,可能就会影响接下来的测试.这才是真正的工作量 ...
 - 基于CentOS 搭建 FTP 文件服务
			
系统要求: CentOS 7.2 64 位操作系统 一. 安装 VSFTPD (vsftpd 是在 Linux 上被广泛使用的 FTP 服务器,根据其[官网介绍][https://security.a ...