PDA

View Full Version : How to make QGraphicsItem a background for the QGraphicsScene?



CassioTC
23rd March 2011, 13:24
I need use QGraphicsItem as a background, this background can be changed by the user any time.

In future I will add other items, like icons.

I'm using a 1680x1050 image, but will have to support any size.

My problem is: the image background being shown in diferent positions and sizes in FullScreen mode e maximized mode and in both cases with a wrong size and position.

mainview.h


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

Background *bg = new Background();
scene->addItem(bg);
setScene(scene);

setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setViewportUpdateMode(FullViewportUpdate);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
}

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



mainview.cpp


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

signals:

public slots:

private:
QGraphicsView *view;
QGraphicsScene *scene;

protected:
void resizeEvent(QResizeEvent *event);
};


background.h


class Background : public QGraphicsItem
{
public:
Background(QGraphicsItem *parent=0);

QRectF boundingRect() const
{
return QRectF(0,0,QApplication::desktop()->width(),QApplication::desktop()->height());
}

private:
QPixmap *backgroundImage;

protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawPixmap(boundingRect().toRect(),*backgroundImag e);
}
};


background.cpp


Background::Background(QGraphicsItem *parent)
{
backgroundImage = new QPixmap();
backgroundImage->load("C:/wallpaper.jpg");
}

high_flyer
23rd March 2011, 14:27
You should not do that, as it makes no sense to have a top level item as background item.
The scene should provide the background, and it does.
See:
http://doc.qt.nokia.com/latest/qgraphicsscene.html#drawBackground
for more details.

CassioTC
23rd March 2011, 15:16
Ok.

I comment this lines:



//Background *bg = new Background();
//scene->addItem(bg);

//setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
//scene->setSceneRect(0,0, view->width(), view->height());


and add this:



void MainView::drawBackground(QPainter *painter, const QRectF &rect)
{
QImage img;
img.load("C:/wallpaper.jpg");

QRectF r;
r.setRect(0,0,img.width(),img.height());

painter->drawImage(r,img);
}


But why is so slow??

high_flyer
23rd March 2011, 15:38
because you load the image every time the background is being drawn.
Load the image somewhere else, store it in a member and use only the member in drawBackground()

CassioTC
23rd March 2011, 16:22
Thank you.
Now works fine