在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设置图片自适应大小的更多相关文章

  1. css控制图片自适应大小

    相信大家做网页时经常会碰到大分辨率的图片会把表格涨破以致漂亮的网页面目全非,但只要使用以下的CSS语句即可解决.      该CSS的功能是:大于600的图片自动调整为600显示. <style ...

  2. iOS HTML 字符串中的图片 自适应大小

    本文原文地址:http://www.cnblogs.com/qianLL/p/6095988.html 有时候 我们接收数据的时候  后台给的数据室一串HTML 的字符串  但是 我们要显示出来  这 ...

  3. Android代码中动态设置图片的大小(自动缩放),位置

    项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下: 1.布局文件 <RelativeLayout xmlns:android=&qu ...

  4. Android drawableleft drawableTop 设置图片的大小

    例子: Drawable drawable=getResources().getDrawable(R.drawable.xx); //获取图片 drawable.setBounds(left, top ...

  5. 设置图片自适应DIV大小

    可以利用CSS样式表中表示后代的复合选择器进行设置.例: <head> <style type="text/css"> #right img /*设定box ...

  6. android设置图片自适应控件大小

    在XML文件的ImageView属性中加上:android:scaleType="fitXY"

  7. word 2013 粘贴的图片自适应大小

    1.先切换到页面视图 2.粘贴图片进去,成功自适应,像素不变,可右键图片另存为图片,查看原始图片,或者ctrl+滚轮上放大. 3.在其他视图就会出现超出范围的情况,还要自己调整

  8. 纯js实现div内图片自适应大小

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Winfrom PictureBox 设置图片自适应

    初始状态 Bitmap bm = new Bitmap(Image.FromStream(System.Net.WebRequest.Create(new Uri(result.Result)).Ge ...

随机推荐

  1. 给你的网站添加 console.js

    本文仅先给使用console调试的FE同学,如果你还不知道console是什么,或者还停留在alert阶段,那就不要浪费时间了,say bay bay! 你是否试程序的过程中用过console.log ...

  2. Centos 7 安装 Mysql 5.5 5.6 5.7

    环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# uname -a Linu ...

  3. zookeepercli - Command Line Interface for ZooKeeper

    简介 ZooKeeper命令行界面(CLI)用于与ZooKeeper进行交互以用于开发目的.它对于调试很有用. 要执行ZooKeeper CLI操作,首先打开你的ZooKeeper服务器(“bin / ...

  4. SSH使用自定义私钥进行登录

    ssh -i /root/.ssh/id_rsa root@192.168.1.2 -i指定了私钥文件的路径

  5. Java 多线程(七) 线程间的通信——wait及notify方法

    线程间的相互作用 线程间的相互作用:线程之间需要一些协调通信,来共同完成一件任务. Object类中相关的方法有两个notify方法和三个wait方法: http://docs.oracle.com/ ...

  6. Android:TextView控件

    3.2.1    TextView TextView 可以说是 Android 中最简单的一个控件了,你在前面其实也已经和它打过了一 些打交道.它主要用于在界面上显示一段文本信息,比如你在第一章看到的 ...

  7. eclipse Specified VM install not found: type Standard VM, name

    运行ant的时候出现 Specified VM install not found: type Standard VM, name.... 尝试删除这些文件: ... / .metadata / .p ...

  8. 解决MySQL8.0报错:Unknown system variable 'validate_password_policy'

    一.问题描述 1.在安装MySQL8.0时,修改临时密码,因密码过于简单(如:123456),不符合MySQL密码规范,会触发一个报错信息: ERROR 1819 (HY000): Your pass ...

  9. Android平台上最好的几款免费的代码编辑器

    使用正确的开发工具能够快速有效地完成源代码的编写和测试,使编程事半功倍.在网络信息高速发展的今天,移动设备的方便快捷已经深入人心,越来越多的程序员会选择在任何感觉舒适的地方使用移动设备查看或者编辑源代 ...

  10. 【SqlServer】SqlServer的常规操作

    创建一张新表,不负责任何数据(该表不会有原来表的主键.索引等等) select * into NewTable from OldTable where 1<>1; 创建一张新表,并且复制旧 ...