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.

Scaling QPixmap image

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
Symbian
Article
Keywords: QPixmap
Created: tepaa (26 Mar 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This code snippets shows how to scale a QPixmap image.


Preconditions

Header

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

Source

The application supports screen orientation changes. In resizeEvent(), store the new size of the widget. This way the image can always be scaled to the correct size.

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

Load the image from the file system:

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

Scale the new image and draw it on the screen.

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);
}
}

Postconditions

The QPixmap image has been scaled.

Related articles

Scaling QGraphicsPixmapItem

This page was last modified on 11 October 2012, at 01:18.
284 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.

×