xtoolbuttonex.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "xtoolbuttonex.h"
  2. XToolButtonEx::XToolButtonEx(QWidget *parent) :
  3. QToolButton(parent)
  4. {
  5. initVariable();
  6. }
  7. void XToolButtonEx::setMouseEnterFlag(bool flag)
  8. {
  9. m_mouseEnterFlag = flag;
  10. this->update();
  11. }
  12. void XToolButtonEx::setMousePressFlag(bool flag)
  13. {
  14. m_mousePressFlag = flag;
  15. this->update();
  16. }
  17. void XToolButtonEx::enterEvent(QEvent *e)
  18. {
  19. if (!m_mousePressFlag)
  20. {
  21. setMouseEnterFlag(true);
  22. }
  23. this->setCursor(Qt::PointingHandCursor);
  24. }
  25. void XToolButtonEx::leaveEvent(QEvent *e)
  26. {
  27. setMouseEnterFlag(false);
  28. }
  29. void XToolButtonEx::mousePressEvent(QMouseEvent *e)
  30. {
  31. if (e->button() == Qt::LeftButton)
  32. {
  33. setMousePressFlag(true);
  34. emit signalLabelPress(this);
  35. }
  36. }
  37. void XToolButtonEx::paintEvent(QPaintEvent *e)
  38. {
  39. QPainter painter(this);
  40. if (m_mouseEnterFlag)
  41. {
  42. paintWidget(50,100, &painter);
  43. }
  44. else if (m_mousePressFlag)
  45. {
  46. paintWidget(150,200, &painter);
  47. }
  48. QToolButton::paintEvent(e);
  49. }
  50. void XToolButtonEx::initVariable()
  51. {
  52. QPalette objPalette = palette();
  53. objPalette.setColor(QPalette::ButtonText, QColor(220,220,220));
  54. setPalette(objPalette);
  55. setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  56. setStyleSheet("QToolButton{border:0px;font:10px}");
  57. setIconSize(QSize(TOOLWIDGET_W ,TOOLWIDGET_H));
  58. setFixedSize(TOOLWIDGET_W+30,TOOLWIDGET_H+30);
  59. setMaximumSize(TOOLWIDGET_W+30,TOOLWIDGET_H+30);
  60. setMinimumSize(TOOLWIDGET_W+30,TOOLWIDGET_H+30);
  61. setMouseEnterFlag(false);
  62. setMousePressFlag(false);
  63. //setAutoRaise(true);
  64. }
  65. void XToolButtonEx::paintWidget(int nTopPartOpacity,int nBottomPartOpacity, QPainter *device)
  66. {
  67. QPen pen(Qt::NoBrush,1);
  68. device->setPen(pen);
  69. QLinearGradient linear(this->rect().topLeft(), this->rect().bottomLeft());
  70. linear.setColorAt(0,QColor(255,255,255,nTopPartOpacity));
  71. linear.setColorAt(0.5,QColor(50,50,50,255));
  72. linear.setColorAt(1,QColor(255,255,255,nBottomPartOpacity));
  73. QBrush brush(linear);
  74. //device->setBrush(brush);
  75. QRect re;
  76. re.setTopLeft(QPoint(this->rect().topLeft().x()+1,this->rect().topLeft().y()+1));
  77. re.setBottomLeft(QPoint(this->rect().bottomLeft().x()+1,this->rect().bottomLeft().y()-1));
  78. re.setTopRight(QPoint(this->rect().topRight().x()-1,this->rect().topRight().y()+1));
  79. re.setBottomRight(QPoint(this->rect().bottomRight().x()-1,this->rect().bottomRight().y()-1));
  80. device->drawRoundedRect(this->rect(), 5, 5);
  81. QPen pen2(Qt::NoBrush,1);
  82. device->setPen(pen2);
  83. device->drawRoundedRect(re, 5, 5);
  84. }