Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

缩放QPixmap图像

From Wiki
Jump to: navigation, search
Article Metadata

兼容于
平台:
Symbian

文章
dougcn 在 04 Apr 2009 创建
最后由 hamishwillee 在 11 Oct 2012 编辑
  • 平台:Qt
  • 设备:5800 XpressMusic
  • 类别:Qt for Symbian
  • 子类别:UI
  • 关键字:QPixmap

Contents

概述

这个代码片段演示如何缩放QPixmap图像。

:要使用这段代码,在你的平台上要装有Qt。


前提条件

头文件

public:
void updateImage();
void paintEvent(QPaintEvent*);
void resizeEvent (QResizeEvent*);
 
private: //Data
QPixmap* pixmap;
QSize widgetSize;

源代码

将widget新大小保存起来。应用程序支持屏幕方向改变,图片总是正确的大小。

void QMyWidget::resizeEvent (QResizeEvent* event)
{
widgetSize = event->size();
// Call base class impl
QWidget::resizeEvent(event);
}

从文件系统装入图形。

void QMyWidget::updateImage()
{
delete pixmap;
pixmap = 0;
// imagePath can be something like "c:/data/Images/myImage.jpg"
pixmap = new QPixmap(imagePath);
update();
}

缩放新图像,然后绘制到屏幕。

void QMyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
 
if (!pixmap->isNull())
{
QPoint centerPoint(0,0);
// Scale new image which size is widgetSize
QPixmap scaledPixmap = pixmap->scaled(widgetSize, Qt::KeepAspectRatio);
// Calculate image center position into screen
centerPoint.setX((widgetSize.width()-scaledPixmap.width())/2);
centerPoint.setY((widgetSize.height()-scaledPixmap.height())/2);
// Draw image
painter.drawPixmap(centerPoint,scaledPixmap);
}
}


后置条件

QPixmap被缩放刚好适用屏幕。

This page was last modified on 11 October 2012, at 01:19.
81 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×