您的位置:首页 > 编程语言 > Qt开发

QT实现透明效果的按钮

2010-09-09 22:54 330 查看
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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: