ImageWidget_nodrag.cpp 914 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "ImageWidget_nodrag.h"
  2. #include "ui_ImageWidget_nodrag.h"
  3. #include <QPainter>
  4. ImageWidget_nodrag::ImageWidget_nodrag(QWidget *parent)
  5. : QWidget(parent)
  6. , ui(new Ui::ImageWidget_nodrag),
  7. imageOffset(0, 0)
  8. {
  9. ui->setupUi(this);
  10. }
  11. ImageWidget_nodrag::~ImageWidget_nodrag()
  12. {
  13. delete ui;
  14. }
  15. void ImageWidget_nodrag::setPixmap(const QPixmap& newPixmap)
  16. {
  17. this->pixmap = newPixmap;
  18. imageOffset = QPoint(0, 0);
  19. update();
  20. }
  21. void ImageWidget_nodrag::paintEvent(QPaintEvent *event)
  22. {
  23. QPainter painter(this);
  24. if (!pixmap.isNull())
  25. {
  26. int centerX = width() / 2;
  27. int centerY = height() / 2;
  28. int pixmapCenterX = pixmap.width() / 2;
  29. int pixmapCenterY = pixmap.height() / 2;
  30. int x = centerX - pixmapCenterX + imageOffset.x();
  31. int y = centerY - pixmapCenterY + imageOffset.y();
  32. painter.drawPixmap(x, y, pixmap);
  33. }
  34. }