PDA

View Full Version : How to resize a background imagem in the QGraphicsScene?



CassioTC
23rd March 2011, 23:57
How can I resize the background image to fit in the scene background?

The image is shown out of the scene.


mainview.cpp


MainView::MainView(QGraphicsView *parent)
: QGraphicsView(parent)
{
view = new QGraphicsView();
scene = new QGraphicsScene(0,0,QApplication::desktop()->width(),QApplication::desktop()->height());

image = new QImage("C:/fundo_01.png");
QImage lampada_on, lampada_off, lampada_on2, lampada_off2;
lampada_on.load("C:/lampada_on.png");
lampada_off.load("C:/lampada_off.png");
lampada_on2.load("C:/lampada_on2.png");
lampada_off2.load("C:/lampada_off2.png");


QAbstractGraphicsShapeItem* i = new Icon(lampada_on2, lampada_off2);
QAbstractGraphicsShapeItem* j = new Icon(lampada_on, lampada_off);
scene->addItem(i);
scene->addItem(j);

setScene(scene);

setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
}

void MainView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(QRectF(0,0,scene->width(),scene->height()), Qt::IgnoreAspectRatio);
}

void MainView::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->setRenderHint(QPainter::SmoothPixmapTransform);
QRectF r;

r.setRect(0,0,image->width(),image->height());
painter->drawImage(r,*image);
}



mainview.h


class MainView : public QGraphicsView
{
Q_OBJECT
public:
MainView(QGraphicsView *parent=0);
~MainView() {};

private:
QGraphicsView *view;
QGraphicsScene *scene;
QImage *image;//, *lampada_on, *lampada_off, *lampada_on2, *lampada_off2;

protected:
void resizeEvent(QResizeEvent *event);
void drawBackground(QPainter *painter, const QRectF &rect);
};

pradeeps
24th March 2011, 04:29
Try the scaling function in graphics view.


void QGraphicsView::scale ( qreal sx, qreal sy )