Results 1 to 3 of 3

Thread: Show background image to QGraphicsView

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Show background image to QGraphicsView

    Good morning,
    I would have a help in showing a background image to a QGraphicsView.
    I have an app develop to edit images and before displaying the images that have to be edited I would display a logo.

    Reading docs I think to use the QGraphicsView::drawBackground so in brief this is what I did

    Qt Code:
    1. // Ctor of my class derived from QGraphicsView
    2. PageView::PageView(QWidget *parent) : QGraphicsView(parent)
    3. {
    4. // ...
    5. // more code
    6. if ( m_logoPixmap.load(":/Resources/Logos/editor_logo_v.png"))
    7. {
    8. // scale it
    9. m_logoScaledPixmap = m_logoPixmap.scaled(QSize(viewport()->width() / 3, viewport()->height() / 3), Qt::KeepAspectRatio, Qt::SmoothTransformation); // Incorrect size
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    I would scale it to be a percentage of the view but i can not get the correct size ( I always get 100, 30 )

    Qt Code:
    1. // Here I would show the logo
    2. void PageView::drawBackground(QPainter *painter, const QRectF &rect)
    3. {
    4. QRectF pixmapBoundingRect = m_logoScaledPixmap.rect();
    5. qreal maxValue = qMax(pixmapBoundingRect.width(), pixmapBoundingRect.height());
    6.  
    7. painter->drawPixmap(pixmapBoundingRect, m_logoScaledPixmap, QRect(0.0, 0.0, m_logoScaledPixmap.width(), m_logoScaledPixmap.height())); // Not centered
    8. }
    To copy to clipboard, switch view to plain text mode 

    I would the logo to be centered and is also pixeled when scaled even adding Qt::SmoothTransformation ( its original size is 2000 * 2000 pixels ).
    I also would the logo to disappear when I load images to be edited

    Thanx
    Franco Amato

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Show background image to QGraphicsView

    Hope this helps
    Qt Code:
    1. class GraphicsView : public QGraphicsView
    2. {
    3. public:
    4. explicit GraphicsView(const QImage & background, QWidget * parent = 0)
    5. : QGraphicsView(parent)
    6. , mImage(background){ }
    7.  
    8. protected:
    9. void paintEvent(QPaintEvent * event) override
    10. {
    11. QPainter painter(viewport());
    12. const QImage image = mImage.scaled(viewport()->size()/3);
    13. const QPoint point = viewport()->rect().center() - QPoint(image.width()/2, image.height()/2);
    14. painter.drawImage(point, image);
    15. QGraphicsView::paintEvent(event);
    16. }
    17.  
    18. private:
    19. const QImage mImage;
    20. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Show background image to QGraphicsView

    Hi thank you so much for your reply, but unfortunately it does not solve my problem.

    I got a working solution in this way:


    Qt Code:
    1. void PageView::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. QRectF imageBoundingRect = m_scaledLogoImage.rect();
    4. QRectF target(QPoint(-200,-200),QSize(imageBoundingRect.width(), imageBoundingRect.height())); // Considering the size of the image is 400*400
    5.  
    6. painter->save();
    7. painter->drawImage(target, m_scaledLogoImage);
    8. painter->restore();
    9. }
    To copy to clipboard, switch view to plain text mode 

    I have another question. I resize the image in the ctor of the class
    Qt Code:
    1. if (m_logoImage.load(":/Resources/editor_logo_v.png");
    2. {
    3. m_scaledLogoImage = m_logoImage.scaled(QSize(400,400), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    4. }
    To copy to clipboard, switch view to plain text mode 

    and I have to rescale to a fixed size but I would rescale to a percentage of the view and I can not because the size() always returns (100, 30)

    Do you have any idea why?

    Thank you
    Last edited by franco.amato; 31st March 2017 at 15:36. Reason: Did a mistake
    Franco Amato

Similar Threads

  1. Replies: 4
    Last Post: 27th November 2013, 15:15
  2. Replies: 0
    Last Post: 5th April 2011, 13:36
  3. Replies: 0
    Last Post: 14th July 2010, 13:48
  4. Replies: 5
    Last Post: 16th December 2009, 11:33
  5. Replies: 0
    Last Post: 6th April 2009, 01:20

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.