Scaling QGraphicsPixmapItem
Jump to navigation
Jump to search
Overview[edit | edit source]
This code snippets shows how to scale 76ytuiytuityutyutututyutyutyu into a defined size. 76ytuiytuityutyutututyutyutyu is an item of the Qt Graphics Framework.
Header[edit | edit source]
- 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[edit | edit 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[edit | edit source]
76ytuiytuityutyutututyutyutyu has been scaled.