PDA

View Full Version : Show background image to QGraphicsView



franco.amato
30th March 2017, 21:35
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


// Ctor of my class derived from QGraphicsView
PageView::PageView(QWidget *parent) : QGraphicsView(parent)
{
// ...
// more code
if ( m_logoPixmap.load(":/Resources/Logos/editor_logo_v.png"))
{
// scale it
m_logoScaledPixmap = m_logoPixmap.scaled(QSize(viewport()->width() / 3, viewport()->height() / 3), Qt::KeepAspectRatio, Qt::SmoothTransformation); // Incorrect size
}
}

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


// Here I would show the logo
void PageView::drawBackground(QPainter *painter, const QRectF &rect)
{
QRectF pixmapBoundingRect = m_logoScaledPixmap.rect();
qreal maxValue = qMax(pixmapBoundingRect.width(), pixmapBoundingRect.height());

painter->drawPixmap(pixmapBoundingRect, m_logoScaledPixmap, QRect(0.0, 0.0, m_logoScaledPixmap.width(), m_logoScaledPixmap.height())); // Not centered
}

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

Santosh Reddy
31st March 2017, 13:06
Hope this helps


class GraphicsView : public QGraphicsView
{
public:
explicit GraphicsView(const QImage & background, QWidget * parent = 0)
: QGraphicsView(parent)
, mImage(background){ }

protected:
void paintEvent(QPaintEvent * event) override
{
QPainter painter(viewport());
const QImage image = mImage.scaled(viewport()->size()/3);
const QPoint point = viewport()->rect().center() - QPoint(image.width()/2, image.height()/2);
painter.drawImage(point, image);
QGraphicsView::paintEvent(event);
}

private:
const QImage mImage;
};

franco.amato
31st March 2017, 15:47
Hi thank you so much for your reply, but unfortunately it does not solve my problem.

I got a working solution in this way:



void PageView::drawBackground(QPainter *painter, const QRectF &rect)
{
QRectF imageBoundingRect = m_scaledLogoImage.rect();
QRectF target(QPoint(-200,-200),QSize(imageBoundingRect.width(), imageBoundingRect.height())); // Considering the size of the image is 400*400

painter->save();
painter->drawImage(target, m_scaledLogoImage);
painter->restore();
}

I have another question. I resize the image in the ctor of the class


if (m_logoImage.load(":/Resources/editor_logo_v.png");
{
m_scaledLogoImage = m_logoImage.scaled(QSize(400,400), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

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