Results 1 to 2 of 2

Thread: How to resize a background imagem in the QGraphicsScene?

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

    Lightbulb How to resize a background imagem in the QGraphicsScene?

    How can I resize the background image to fit in the scene background?

    The image is shown out of the scene.


    mainview.cpp
    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. image = new QImage("C:/fundo_01.png");
    8. QImage lampada_on, lampada_off, lampada_on2, lampada_off2;
    9. lampada_on.load("C:/lampada_on.png");
    10. lampada_off.load("C:/lampada_off.png");
    11. lampada_on2.load("C:/lampada_on2.png");
    12. lampada_off2.load("C:/lampada_off2.png");
    13.  
    14.  
    15. QAbstractGraphicsShapeItem* i = new Icon(lampada_on2, lampada_off2);
    16. QAbstractGraphicsShapeItem* j = new Icon(lampada_on, lampada_off);
    17. scene->addItem(i);
    18. scene->addItem(j);
    19.  
    20. setScene(scene);
    21.  
    22. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    23. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    24. }
    25.  
    26. void MainView::resizeEvent(QResizeEvent *event)
    27. {
    28. QGraphicsView::resizeEvent(event);
    29. fitInView(QRectF(0,0,scene->width(),scene->height()), Qt::IgnoreAspectRatio);
    30. }
    31.  
    32. void MainView::drawBackground(QPainter *painter, const QRectF &rect)
    33. {
    34. painter->setRenderHint(QPainter::SmoothPixmapTransform);
    35. QRectF r;
    36.  
    37. r.setRect(0,0,image->width(),image->height());
    38. painter->drawImage(r,*image);
    39. }
    To copy to clipboard, switch view to plain text mode 


    mainview.h
    Qt Code:
    1. class MainView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4. public:
    5. MainView(QGraphicsView *parent=0);
    6. ~MainView() {};
    7.  
    8. private:
    9. QImage *image;//, *lampada_on, *lampada_off, *lampada_on2, *lampada_off2;
    10.  
    11. protected:
    12. void resizeEvent(QResizeEvent *event);
    13. void drawBackground(QPainter *painter, const QRectF &rect);
    14. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to resize a background imagem in the QGraphicsScene?

    Try the scaling function in graphics view.

    Qt Code:
    1. void QGraphicsView::scale ( qreal sx, qreal sy )
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to resize a QGraphicsScene to a QGraphicsView?
    By CassioTC in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2011, 10:03
  2. QgraphicsScene on QGLWidget background
    By ksrini in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2009, 10:57
  3. QGraphicsScene background
    By goldhappywang in forum Newbie
    Replies: 2
    Last Post: 28th April 2009, 08:57
  4. OpenGL texture on QGraphicsScene background
    By Ovnan in forum Qt Programming
    Replies: 5
    Last Post: 11th July 2008, 11:39
  5. QGraphicsScene / QGraphicsView speed after resize
    By themolecule in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 00:46

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.