×
Namespaces

Variants
Actions

Scaling QGraphicsPixmapItem

From Nokia Developer Wiki
Jump to: navigation, search
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: hamishwillee (11 Oct 2012)

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 11 October 2012, at 04:18.
99 page views in the last 30 days.
×