×
Namespaces

Variants
Actions

Scaling QGraphicsPixmapItem

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QGraphicsPixmapItem
Created: tepaa (17 Apr 2009)
Last edited: lpvalente (16 Aug 2014)

Contents

Overview

This code snippets shows how to scale QGraphicsPixmapItem into a defined size. QGraphicsPixmapItem is an item of the Qt Graphics Framework.


Header

#include <QGraphicsPixmapItem>
class MyPixmapItem: public QGraphicsPixmapItem
{
public:
MyPixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem=0, int size=0);
~MyPixmapItem();
 
public:
QRectF boundingRect() const;
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget=0);
void setSize(int size);
 
private:
int m_size;
};


Source

MyPixmapItem::MyPixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem, int size)
: QGraphicsPixmapItem(pixmap,parentItem)
{
setCacheMode(NoCache);
setSize(size);
}
 
MyPixmapItem::~MyPixmapItem()
{
}
 
void MyPixmapItem::setSize(int size)
{
this->m_size = size;
}
 
QRectF MyPixmapItem::boundingRect() const
{
// Return defined 'size'
return QRectF(QPointF(0,0),QSizeF(m_size,m_size));
}
 
void MyPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
// Scale QGraphicsPixmapItem to wanted 'size' and keep the aspect ratio using boundingRect()
painter->drawPixmap(boundingRect().toRect(), pixmap());
 
// NOTE: Does not use base class paint for painting now, that does not scale QPixmap
//QGraphicsPixmapItem::paint(painter, option, widget);
}

Using MyPixmapItem can be implemented as shown:

    QGraphicsScene* m_graphicsScene = new QGraphicsScene();
QGraphicsView* m_graphicsView = new QGraphicsView(m_graphicsScene, this);
m_graphicsScene->setSceneRect(this->rect());
 
// Create MyPixmapItem from png from the same directory where exe file is
QPixmap p(qApp->applicationDirPath() + QDir::separator() +"candle.png");
MyPixmapItem* pixmapItem = new MyPixmapItem(p,0,p.width()*0.5);
m_graphicsScene->addItem(pixmapItem)

Postconditions

QGraphicsPixmapItem has been scaled.

Related articles

Scaling QPixmap image

This page was last modified on 16 August 2014, at 02:55.
119 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.

×