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");
}
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");
}