| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #include "xtoolbuttonex.h"
- XToolButtonEx::XToolButtonEx(QWidget *parent) :
- QToolButton(parent)
- {
- initVariable();
- }
- void XToolButtonEx::setMouseEnterFlag(bool flag)
- {
- m_mouseEnterFlag = flag;
- this->update();
- }
- void XToolButtonEx::setMousePressFlag(bool flag)
- {
- m_mousePressFlag = flag;
- this->update();
- }
- void XToolButtonEx::enterEvent(QEvent *e)
- {
- if (!m_mousePressFlag)
- {
- setMouseEnterFlag(true);
- }
- this->setCursor(Qt::PointingHandCursor);
- }
- void XToolButtonEx::leaveEvent(QEvent *e)
- {
- setMouseEnterFlag(false);
- }
- void XToolButtonEx::mousePressEvent(QMouseEvent *e)
- {
- if (e->button() == Qt::LeftButton)
- {
- setMousePressFlag(true);
- emit signalLabelPress(this);
- }
- }
- void XToolButtonEx::paintEvent(QPaintEvent *e)
- {
- QPainter painter(this);
- if (m_mouseEnterFlag)
- {
- paintWidget(50,100, &painter);
- }
- else if (m_mousePressFlag)
- {
- paintWidget(150,200, &painter);
- }
- QToolButton::paintEvent(e);
- }
- void XToolButtonEx::initVariable()
- {
- QPalette objPalette = palette();
- objPalette.setColor(QPalette::ButtonText, QColor(220,220,220));
- setPalette(objPalette);
- setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- setStyleSheet("QToolButton{border:0px;font:10px}");
- setIconSize(QSize(TOOLWIDGET_W ,TOOLWIDGET_H));
- setFixedSize(TOOLWIDGET_W+30,TOOLWIDGET_H+30);
- setMaximumSize(TOOLWIDGET_W+30,TOOLWIDGET_H+30);
- setMinimumSize(TOOLWIDGET_W+30,TOOLWIDGET_H+30);
- setMouseEnterFlag(false);
- setMousePressFlag(false);
- //setAutoRaise(true);
- }
- void XToolButtonEx::paintWidget(int nTopPartOpacity,int nBottomPartOpacity, QPainter *device)
- {
- QPen pen(Qt::NoBrush,1);
- device->setPen(pen);
- QLinearGradient linear(this->rect().topLeft(), this->rect().bottomLeft());
- linear.setColorAt(0,QColor(255,255,255,nTopPartOpacity));
- linear.setColorAt(0.5,QColor(50,50,50,255));
- linear.setColorAt(1,QColor(255,255,255,nBottomPartOpacity));
- QBrush brush(linear);
- //device->setBrush(brush);
- QRect re;
- re.setTopLeft(QPoint(this->rect().topLeft().x()+1,this->rect().topLeft().y()+1));
- re.setBottomLeft(QPoint(this->rect().bottomLeft().x()+1,this->rect().bottomLeft().y()-1));
- re.setTopRight(QPoint(this->rect().topRight().x()-1,this->rect().topRight().y()+1));
- re.setBottomRight(QPoint(this->rect().bottomRight().x()-1,this->rect().bottomRight().y()-1));
- device->drawRoundedRect(this->rect(), 5, 5);
- QPen pen2(Qt::NoBrush,1);
- device->setPen(pen2);
- device->drawRoundedRect(re, 5, 5);
- }
|