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 ...
随机推荐
- php get_magic_quotes_gpc()函数使用
magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post.get.cookie过来的数据增加转义字符"\",以确保这些数据不会引起程序,特别 ...
- 多个gridSelect引用同一个dizData
independence设置为true,如果不设置,下拉没有数据.
- Oracle 删除重复数据只留一条(转)
转自:http://www.cnblogs.com/252e/archive/2012/09/13/2682817.html 查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是 ...
- Spark MLlib 之 aggregate和treeAggregate从原理到应用
在阅读spark mllib源码的时候,发现一个出镜率很高的函数--aggregate和treeAggregate,比如matrix.columnSimilarities()中.为了好好理解这两个方法 ...
- Netbeans异常之cannet locate java installation in specified jdkhome
原因:更改了jdk安装版本 方法:修改netbeans安装目录下ect下netbeans文件, 详细代码位置: # Default location of JDK: # (set by install ...
- AngularJS中的$http缓存以及处理多个$http请求
在AngularJS的实际项目中,经常需要处理多个$http请求,每个$http请求返回一个promise,我们可以把多个promise放到$q.all()方法接受的一个数组实参中去. ■ 处理多个$ ...
- 深入学习 FutureTask
原文出处: 天凉好个秋 第一部分:What 在Java中一般通过继承Thread类或者实现Runnable接口这两种方式来创建多线程,但是这两种方式都有个缺陷,就是不能在执行完成后获取执行的结果,因此 ...
- 【T03】理解私有地址和NAT
1.私有地址包括三块: 10.0.0.0 到 10.255.255.255 172.16.0.0 到 172.31.0.0 192.168.0.0 到 192.168.255.255 2.私有地址接入 ...
- .net跨平台分析软件
https://marketplace.visualstudio.com/items?itemName=ConnieYau.NETPortabilityAnalyzer
- mysql函数和操作符
mysql,); //取模函数 +-----------+ ,) | +-----------+ | +-----------+ row in set (0.00 sec) mysql,); +--- ...