Results 1 to 5 of 5

Thread: How to make QGraphicsItem a background for the QGraphicsScene?

  1. #1
    Join Date
    Mar 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation How to make QGraphicsItem a background for the QGraphicsScene?

    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
    Qt Code:
    1. MainView::MainView(QGraphicsView *parent)
    2. : QGraphicsView(parent)
    3. {
    4. view = new QGraphicsView();
    5. scene = new QGraphicsScene(0,0,QApplication::desktop()->width(),QApplication::desktop()->height());
    6.  
    7. Background *bg = new Background();
    8. scene->addItem(bg);
    9. setScene(scene);
    10.  
    11. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    12. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    13. setViewportUpdateMode(FullViewportUpdate);
    14. setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
    15. }
    16.  
    17. void MainView::resizeEvent(QResizeEvent *event)
    18. {
    19. QGraphicsView::resizeEvent(event);
    20. fitInView(scene->sceneRect(), Qt::IgnoreAspectRatio);
    21. scene->setSceneRect(0,0, view->width(), view->height());
    22. }
    To copy to clipboard, switch view to plain text mode 


    mainview.cpp
    Qt Code:
    1. class MainView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4. public:
    5. MainView(QGraphicsView *parent=0);
    6. ~MainView() {};
    7.  
    8. signals:
    9.  
    10. public slots:
    11.  
    12. private:
    13.  
    14. protected:
    15. void resizeEvent(QResizeEvent *event);
    16. };
    To copy to clipboard, switch view to plain text mode 

    background.h
    Qt Code:
    1. class Background : public QGraphicsItem
    2. {
    3. public:
    4. Background(QGraphicsItem *parent=0);
    5.  
    6. QRectF boundingRect() const
    7. {
    8. return QRectF(0,0,QApplication::desktop()->width(),QApplication::desktop()->height());
    9. }
    10.  
    11. private:
    12. QPixmap *backgroundImage;
    13.  
    14. protected:
    15. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    16. {
    17. painter->drawPixmap(boundingRect().toRect(),*backgroundImage);
    18. }
    19. };
    To copy to clipboard, switch view to plain text mode 

    background.cpp
    Qt Code:
    1. Background::Background(QGraphicsItem *parent)
    2. {
    3. backgroundImage = new QPixmap();
    4. backgroundImage->load("C:/wallpaper.jpg");
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to make QGraphicsItem a background for the QGraphicsScene?

    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/qgrap...drawBackground
    for more details.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: How to make QGraphicsItem a background for the QGraphicsScene?

    Ok.

    I comment this lines:

    Qt Code:
    1. //Background *bg = new Background();
    2. //scene->addItem(bg);
    3.  
    4. //setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
    5. //scene->setSceneRect(0,0, view->width(), view->height());
    To copy to clipboard, switch view to plain text mode 

    and add this:

    Qt Code:
    1. void MainView::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. QImage img;
    4. img.load("C:/wallpaper.jpg");
    5.  
    6. QRectF r;
    7. r.setRect(0,0,img.width(),img.height());
    8.  
    9. painter->drawImage(r,img);
    10. }
    To copy to clipboard, switch view to plain text mode 

    But why is so slow??

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to make QGraphicsItem a background for the QGraphicsScene?

    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()
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Mar 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make QGraphicsItem a background for the QGraphicsScene?

    Thank you.
    Now works fine

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. QgraphicsScene on QGLWidget background
    By ksrini in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2009, 09:57
  3. QGraphicsScene background
    By goldhappywang in forum Newbie
    Replies: 2
    Last Post: 28th April 2009, 07:57
  4. QGraphicsItem-QGraphicsScene
    By hgedek in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2007, 12:35
  5. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.