当前位置:   article > 正文

带有下划线的按钮_qt toolbutton下划线

qt toolbutton下划线

继承QLabel,话不多说,直接上代码 

underlinebutton.h
  1. #ifndef UNDERLINEBUTTON_H
  2. #define UNDERLINEBUTTON_H
  3. #include <QLabel>
  4. #include <QMouseEvent>
  5. class UnderlineButton : public QLabel
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit UnderlineButton(QWidget *parent = 0);
  10. explicit UnderlineButton(const QString &text, QWidget *parent=0);
  11. bool isPress;
  12. Q_SIGNALS:
  13. void clicked();
  14. public slots:
  15. bool enterBtn(QPoint pp);
  16. protected:
  17. void mousePressEvent(QMouseEvent *event);
  18. void mouseReleaseEvent(QMouseEvent *event);
  19. };
  20. #endif // UNDERLINEBUTTON_H
underlinebutton.cpp
  1. #include "underlinebutton.h"
  2. UnderlineButton::UnderlineButton(QWidget *parent) : QLabel(parent)
  3. {
  4. isPress = false;
  5. }
  6. UnderlineButton::UnderlineButton(const QString &text, QWidget *parent) : QLabel(parent)
  7. {
  8. setText("<U>" + text + "</U>");
  9. setStyleSheet("color:blue");
  10. }
  11. bool UnderlineButton::enterBtn(QPoint pp)
  12. {
  13. int height = this->height();
  14. int width = this->width();
  15. QPoint btnMaxPos;
  16. btnMaxPos.setX(width);
  17. btnMaxPos.setY(height);
  18. if(pp.x() >= 0 && pp.y() >= 0 && pp.x() <= btnMaxPos.x() && pp.y() <= btnMaxPos.y())
  19. return true;
  20. else
  21. return false;
  22. }
  23. void UnderlineButton::mousePressEvent(QMouseEvent *event)
  24. {
  25. if(event->button() == Qt::LeftButton)
  26. {
  27. isPress = true;
  28. setStyleSheet("color:red");
  29. }
  30. }
  31. void UnderlineButton::mouseReleaseEvent(QMouseEvent *event)
  32. {
  33. if(event->button() == Qt::LeftButton)
  34. {
  35. isPress = false;
  36. setStyleSheet("color:blue");
  37. if (enterBtn(event->pos()))
  38. {
  39. emit clicked();
  40. }
  41. }
  42. }

效果图:

可当按钮使用

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/292667
推荐阅读
相关标签
  

闽ICP备14008679号