QT实现透明效果的按钮

typedef struct OpacityBtnInfoSet_Tag

{
    QRect rect;
    QColor color;
    QString strTime;
    QString strHall;
    OpacityBtnInfoSet_Tag()
    {
        strTime = "";
        strHall = "";
    }
    OpacityBtnInfoSet_Tag(OpacityBtnInfoSet_Tag& Info)
    {
        rect.setX(Info.rect.x());
        rect.setY(Info.rect.y());
        rect.setWidth(Info.rect.width());
        rect.setHeight(Info.rect.height());
        color = Info.color;
        strTime = Info.strTime;
        strHall = Info.strHall;
    }
} OpacityBtnInfoSet;
class COpacityButton : public QPushButton
//class ImgButton : public QLabel
{
    Q_OBJECT
public:
    COpacityButton(OpacityBtnInfoSet& Info, QWidget *parent = NULL);
    ~COpacityButton();
    void SetButtonInfo(OpacityBtnInfoSet& Info);
    void GetButtonInfo(OpacityBtnInfoSet& Info);
private slots:
   
private:
    OpacityBtnInfoSet m_BtnInfoSet;
protected:
   virtual void paintEvent(QPaintEvent *event);
   void mousePressEvent(QMouseEvent *event);
   void mouseReleaseEvent(QMouseEvent *event);
};
 
COpacityButton::COpacityButton(OpacityBtnInfoSet& Info, QWidget *parent)
    : QPushButton(parent),m_BtnInfoSet(Info)
{
    if(Info.rect.width() > 0 && Info.rect.height() > 0)
    {
        this->setGeometry(m_BtnInfoSet.rect);
        setMaximumSize(m_BtnInfoSet.rect.width(), m_BtnInfoSet.rect.height());
        resize(m_BtnInfoSet.rect.width(), m_BtnInfoSet.rect.height());
    }
}
COpacityButton::~COpacityButton()
{
}
void COpacityButton::SetButtonInfo(OpacityBtnInfoSet& Info)
{
    m_BtnInfoSet.strTime = Info.strTime;
    m_BtnInfoSet.strHall = Info.strHall;
    m_BtnInfoSet.rect.setRect(Info.rect.x(), Info.rect.y(), Info.rect.width(), Info.rect.height());
    if(m_BtnInfoSet.rect.width() > 0 && m_BtnInfoSet.rect.height() > 0)
    {
        this->setGeometry(m_BtnInfoSet.rect);
        setMaximumSize(m_BtnInfoSet.rect.width(), m_BtnInfoSet.rect.height());
        resize(m_BtnInfoSet.rect.width(), m_BtnInfoSet.rect.height());
    }
    m_BtnInfoSet.color = QColor(Info.color);
    update();
}
void COpacityButton::GetButtonInfo(OpacityBtnInfoSet& Info)
{
    Info.strTime = m_BtnInfoSet.strTime;
    Info.strHall = m_BtnInfoSet.strHall;
    Info.rect.setRect(m_BtnInfoSet.rect.x(), m_BtnInfoSet.rect.y(), m_BtnInfoSet.rect.width(), m_BtnInfoSet.rect.height());
    Info.color = QColor(m_BtnInfoSet.color);
}
void COpacityButton::mousePressEvent(QMouseEvent * event)
{
    QPushButton::mousePressEvent(event);
    setMaximumSize(m_BtnInfoSet.rect.width() + 8, m_BtnInfoSet.rect.height() + 8);
    resize(m_BtnInfoSet.rect.width() + 8, m_BtnInfoSet.rect.height() + 8);
    update();
}
void COpacityButton::mouseReleaseEvent(QMouseEvent *event)
{
    QPushButton::mouseReleaseEvent(event);
    setMaximumSize(m_BtnInfoSet.rect.width(), m_BtnInfoSet.rect.height());
    resize(m_BtnInfoSet.rect.width(), m_BtnInfoSet.rect.height());
    update();
}
void COpacityButton::paintEvent(QPaintEvent *event)
{
    QPushButton::paintEvent(event);
    QPainter paint(this);
    int red = m_BtnInfoSet.color.red();
    int green = m_BtnInfoSet.color.green();
    int blue = m_BtnInfoSet.color.blue();
    int x = this->rect().x();
    int y = this->rect().y();
    qreal width = this->rect().width();
    qreal height = this->rect().height();
    QLinearGradient lineGr(x, y, x, y+height);
    lineGr.setColorAt(0.0, QColor(255, 255, 255, 255));
    lineGr.setColorAt(0.02, QColor(red, green, blue, 191));
    lineGr.setColorAt(0.47, QColor(red, green, blue, 191));
    lineGr.setColorAt(0.48, QColor(50, 50, 50, 200));
    lineGr.setColorAt(0.5, QColor(50, 50, 50, 180));
    lineGr.setColorAt(0.8, QColor(red, green, blue, 191));
    lineGr.setColorAt(0.9, QColor(red, green, blue, 191));
    lineGr.setColorAt(1, QColor(255, 255, 255, 255));
    paint.setRenderHint(QPainter::Antialiasing);
    paint.setPen(Qt::NoPen);
    paint.setBrush(lineGr);
    paint.drawRoundedRect(x,y,width, height, 4, 4);
    paint.setPen(QColor(190,190,190,191));
    paint.drawEllipse(x+width - 20, y + 8, 40, height - 16);
    QFont font(tr("宋体"), 20);
    font.setBold(true);
    paint.setFont(font);
    paint.setPen(Qt::white);
    paint.drawText(rect(),Qt::AlignCenter, m_BtnInfoSet.strTime);
    font.setPointSize(15);
    paint.setFont(font);
    paint.drawText(x+width - 20, y + 8, 20, height - 16,
                   Qt::AlignRight | Qt::AlignVCenter, m_BtnInfoSet.strHall);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值