• QLable提升类


    实现:

            1.图片移动、保持纵横比缩放、右键菜单

    1.myLabel.h 

    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3. #include
    4. class MyLabel : public QLabel
    5. {
    6. Q_OBJECT
    7. public:
    8. MyLabel(QWidget *parent = nullptr);
    9. private:
    10. void contextMenuEvent(QContextMenuEvent* e) override;
    11. void mousePressEvent(QMouseEvent* e) override;
    12. void mouseReleaseEvent(QMouseEvent* e) override;
    13. void mouseMoveEvent(QMouseEvent* e) override;
    14. void paintEvent(QPaintEvent* e) override;
    15. void wheelEvent(QWheelEvent* e) override;
    16. double m_zoomValue = 1.0;
    17. bool isPress = false;
    18. int m_XPtInterval = 0;
    19. int m_YPtInterval = 0;
    20. QPoint m_OldPos;
    21. private slots:
    22. void onLoadImage(void);
    23. void onZoomInImage(void);
    24. void onZoomOutImage(void);
    25. void onPresetImage(void);
    26. };
    27. #endif // MYLABEL_H

     2.myLabel.cpp

    1. #include "myLabel.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. MyLabel::MyLabel(QWidget *parent):
    7. QLabel(parent)
    8. {
    9. // setScaledContents(true);
    10. // this->setAlignment(Qt::AlignCenter);
    11. //如果图片太大,这个可以保证图片不会放大
    12. //setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
    13. }
    14. void MyLabel::contextMenuEvent(QContextMenuEvent *e)
    15. {
    16. QPoint pos = e->pos();
    17. pos = this->mapToGlobal(pos);
    18. QMenu *menu = new QMenu(this);
    19. QAction *loadImage = new QAction(tr("图片放大"));
    20. QObject::connect(loadImage, &QAction::triggered, this, &MyLabel::onLoadImage);
    21. menu->addAction(loadImage);
    22. // menu->addSeparator();
    23. QAction *zoomInAction = new QAction(tr("图片居中还原"));
    24. QObject::connect(zoomInAction, &QAction::triggered, this, &MyLabel::onZoomInImage);
    25. menu->addAction(zoomInAction);
    26. QAction *zoomOutAction = new QAction(tr("3"));
    27. QObject::connect(zoomOutAction, &QAction::triggered, this, &MyLabel::onZoomOutImage);
    28. menu->addAction(zoomOutAction);
    29. QAction *presetAction = new QAction(tr("4"));
    30. QObject::connect(presetAction, &QAction::triggered, this, &MyLabel::onPresetImage);
    31. menu->addAction(presetAction);
    32. menu->exec(pos);
    33. // menu->show();
    34. }
    35. void MyLabel::mousePressEvent(QMouseEvent *e)
    36. {
    37. if(e->button() == Qt::LeftButton){
    38. isPress = true;
    39. m_OldPos = e->pos();
    40. }
    41. }
    42. void MyLabel::mouseReleaseEvent(QMouseEvent *e)
    43. {
    44. isPress = false;
    45. this->setCursor(Qt::ArrowCursor);
    46. }
    47. void MyLabel::mouseMoveEvent(QMouseEvent *e)
    48. {
    49. if (!isPress)
    50. return QWidget::mouseMoveEvent(e);
    51. this->setCursor(Qt::SizeAllCursor);
    52. QPoint pos = e->pos();
    53. int xPtInterval = pos.x() - m_OldPos.x();
    54. int yPtInterval = pos.y() - m_OldPos.y();
    55. m_XPtInterval += xPtInterval;
    56. m_YPtInterval += yPtInterval;
    57. m_OldPos = pos;
    58. this->update();
    59. }
    60. void MyLabel::paintEvent(QPaintEvent* e){
    61. if(this->pixmap().isNull())
    62. {
    63. return QLabel::paintEvent(e);
    64. }
    65. QPainter painter(this);
    66. int width = qMin(pixmap().width(), this->width());
    67. int height = width * 1.0 / (pixmap().width() * 1.0 /pixmap().height());
    68. height = qMin(height, this->height());
    69. width = height * 1.0 * (pixmap().width() * 1.0 / pixmap().height());
    70. painter.translate(this->width() / 2+m_XPtInterval, this->height() / 2+m_YPtInterval );
    71. painter.scale(m_zoomValue,m_zoomValue);
    72. QRect rect(-width/2,-height/2,width,height);
    73. painter.drawImage(rect,this->pixmap().toImage());
    74. // return QLabel::paintEvent(e);
    75. }
    76. void MyLabel::wheelEvent(QWheelEvent *e)
    77. {
    78. int p = e->angleDelta().y();
    79. if(p>0)
    80. {
    81. m_zoomValue += 0.1;
    82. update();
    83. }
    84. if(p<0){
    85. m_zoomValue -= 0.1;
    86. update();
    87. }
    88. }
    89. void MyLabel::onLoadImage()
    90. {
    91. MyLabel* label = new MyLabel();
    92. label->setWindowTitle(parent()->objectName());
    93. label->setPixmap(pixmap());
    94. label->showMaximized();
    95. }
    96. void MyLabel::onZoomInImage()
    97. {
    98. m_XPtInterval = 0;
    99. m_YPtInterval = 0;
    100. m_zoomValue = 1.0;
    101. update();
    102. }
    103. void MyLabel::onZoomOutImage()
    104. {
    105. }
    106. void MyLabel::onPresetImage()
    107. {
    108. }

  • 相关阅读:
    Scheduled定时任务
    预约按摩app软件开发定制足浴SPA上们服务小程序
    csharp开发日常之Activator.CreateInstance构造函数生成实例
    手把手分析 lv_config.h 文件
    Ngnix 部署 结束顽固进程
    3. 基本数据类型
    让 Java 打包缩小一大半,Solon v1.9.3 发布
    APS排程助动力电池企业充分利用产能
    时序数据库-13-[IoTDB]的原理解析
    深度学习--神经网络基础(2)
  • 原文地址:https://blog.csdn.net/weixin_66651900/article/details/142105665