QPainter DrawImage CenterAligned
Asked Answered
A

3

6

Is there any way to draw an image on QPainter center aligned? I see QPainter::drawText gives us this provision but drawImage does not. I have one source rect, target rect and an image. when the source size is small the image gets drawn on the left side of the page. I want it to be printed center aligned.

Aleciaaleck answered 23/9, 2013 at 12:19 Comment(0)
W
10

The painter doesn't have a size, but the device() it paints on does. You can use QRect(painter.device()->width(), painter.device()->height()) as the rectangle where you want to center your image in.

Then you'd paint the image centered like so:

QImage source;
QPainter painter(...);
...
QRect rect(source.rect());
QRect devRect(0, 0, painter.device()->width(), painter.device()->height());
rect.moveCenter(devRect.center());
painter.drawImage(rect.topLeft(), source);
Windbag answered 23/9, 2013 at 13:19 Comment(0)
P
2

I would try to do the following (please follow the source code comments):

The sample image that should be drawn

// The image to draw - blue rectangle 100x100.
QImage img(100, 100, QImage::Format_ARGB32);
img.fill(Qt::blue);

In the paint event handler

[..]
QRect source(0, 0, 100, 100);
QRect target(0, 0, 400, 400);

// Calculate the point, where the image should be displayed.
// The center of source rect. should be in the center of target rect.
int deltaX = target.width() - source.width();
int deltaY = target.height() - source.height();

// Just apply coordinates transformation to draw where we need.
painter.translate(deltaX / 2, deltaY / 2);

painter.drawImage(source, img);

Of course you should check whether source rectangle is smaller than the target before applying this approach. I omitted that code for simplicity reasons just to demonstrate how you can center your image.

Poplar answered 23/9, 2013 at 12:36 Comment(0)
G
2

I wanted to show a more complete example with a variable image size that stays within the bounds of the area provided to add to the other great answers.

void ImageView::paintEvent(QPaintEvent*)
{
  if (this->imageBuffer.empty()){ return; }

  double widgetWidth = this->width();
  double widgetHeight = this->height();
  QRectF target(0, 0, widgetWidth, widgetHeight);

  QImage tempQImage = *this->imageBuffer.at(this->imageBuffer.count()-1);
  tempQImage = tempQImage.scaled(rect().size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

  double imageSizeWidth = static_cast<double>(tempQImage.width());
  double imageSizeHeight = static_cast<double>(tempQImage.height());
  QRectF source(0.0, 0.0, imageSizeWidth, imageSizeHeight);

  int deltaX = 0;
  int deltaY = 0;
  if(source.width() < target.width())
      deltaX = target.width() - source.width();
  else
      deltaX = source.width() - target.width();

  if(source.height() < target.height())
      deltaY = target.height() - source.height();
  else
      deltaY = source.height() - target.height();

  QPainter painter(this);
  painter.translate(deltaX / 2, deltaY / 2);

  painter.drawImage(source, tempQImage);
}
Geneviegenevieve answered 26/1, 2015 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.