ImageWidget.h 779 B

123456789101112131415161718192021222324252627282930313233343536
  1. #ifndef IMAGEWIDGET_H
  2. #define IMAGEWIDGET_H
  3. #include <QWidget>
  4. #include <QPixmap>
  5. #include <QMouseEvent>
  6. namespace Ui {
  7. class ImageWidget;
  8. }
  9. class ImageWidget : public QWidget
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit ImageWidget(QWidget *parent = nullptr);
  14. ~ImageWidget();
  15. void setPixmap(const QPixmap& pixmap);
  16. protected:
  17. void paintEvent(QPaintEvent *event) override;
  18. void mousePressEvent(QMouseEvent *event) override;
  19. void mouseMoveEvent(QMouseEvent *event) override;
  20. void mouseReleaseEvent(QMouseEvent *event) override;
  21. private:
  22. Ui::ImageWidget *ui;
  23. QPixmap pixmap;
  24. QPoint lastMousePos; // 上一次鼠标的位置
  25. QPoint imageOffset; // 图片的偏移量
  26. bool isDragging; // 是否正在拖动
  27. };
  28. #endif // IMAGEWIDGET_H