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 ...
随机推荐
- * -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]’
错误描述: * -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object fr ...
- Django的使用规则
ORM应该算是Python的一大特色,功能类似于Mybatis或hibernate,但是显示要强大更多 一:terminal下命令行的使用 1.创建一个Project django-admin sta ...
- Spark Client启动原理探索
经过几天闲暇时间的学习,终于又理解的深入了一些,关于Spark Client如何提交作业也更清晰了点. 在整体的流程图上是这样的: 大体的思路就是应用程序通过SparkSubmit提交程序后,自动在当 ...
- Structured Streaming教程(3) —— 与Kafka的集成
Structured Streaming最主要的生产环境应用场景就是配合kafka做实时处理,不过在Strucured Streaming中kafka的版本要求相对搞一些,只支持0.10及以上的版本. ...
- Knockout.Js官网学习(加载或保存JSON数据)
前言 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地存储需要序列化数据),交换数据最方便的就是使用JSON格式 – 大多数的Ajax应用程 ...
- C# Queue 和Stack的实现
Queue 和Stack的使用就不用多说吧,一个是先进先出,一个是后进先出. 这里我主要关注其实现原理. queue的实现如下: public class Queue<T> : IEnum ...
- android:如何通过自定义工程模板让新建的工程都默认支持lambda表达式
首先参考这篇文章:自定义Android Studio工程模板,了解如何自定义模板 然后结合我们上一篇文章 android: 在android studio中使用retrolambda的步骤的要点, ...
- 解决netaddr不能安装0.7.6以上版本
今天在Jenkins上添加了两个新jobs,但是同事提交代码后的自动测试运行出现错误.查看原因是tox命令在安装包依赖的时候,找不到大于0.7.6以上的版本,但可是我们明明安装过0.7.10了呀. 后 ...
- CentOS 7.4安装Nginx 1.14.0
一.安装所需环境 1.gcc 安装 yum install gcc-c++
- MySQL的reset slave与reset slave all
reset slave是各版本Mysql都有的功能,可以让slave忘记自己在master binary log中的复制位置. reset slave命令主要完成以下工作内容: -删除master.i ...