qt特效按鈕QPushButton
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
//Declaration #include <QtCore> #include <QtGui> class AeroButton : public QPushButton { Q_OBJECT public: AeroButton(QWidget * parent = 0); AeroButton(const QString & text, QWidget * parent = 0); AeroButton(const QIcon & icon, const QString & text, QWidget * parent = 0); virtual ~AeroButton(); void setColor(QColor color) { m_color = color; } void setHighlight(QColor highlight) { m_highlight = highlight; } void setShadow(QColor shadow) { m_shadow = shadow; } //Range: 0.0 [invisible] - 1.0 [opaque] void setOpacity(qreal opacity) { m_opacity = opacity; } //Range: 0 [rectangle] - 99 [oval] void setRoundness(int roundness) { m_roundness = roundness; } protected: void paintEvent(QPaintEvent * pe); void enterEvent(QEvent * e); void leaveEvent(QEvent * e); void mousePressEvent(QMouseEvent * e); void mouseReleaseEvent(QMouseEvent * e); private: QRect calculateIconPosition(QRect button_rect, QSize icon_size); private: bool m_hovered; bool m_pressed; QColor m_color; QColor m_highlight; QColor m_shadow; qreal m_opacity; int m_roundness; }; //Implementation #include "AeroButton.h" AeroButton::AeroButton(QWidget * parent) : QPushButton(parent), m_hovered(false), m_pressed(false), m_color(Qt::gray), m_highlight(Qt::lightGray), m_shadow(Qt::black), m_opacity(1.0), m_roundness(0){} AeroButton::AeroButton(const QString & text, QWidget * parent) : QPushButton(text, parent), m_hovered(false), m_pressed(false), m_color(Qt::gray), m_highlight(Qt::lightGray), m_shadow(Qt::black), m_opacity(1.0), m_roundness(0){} AeroButton::AeroButton(const QIcon & icon, const QString & text, QWidget * parent) : QPushButton(icon, text, parent), m_hovered(false), m_pressed(false), m_color(Qt::gray), m_highlight(Qt::lightGray), m_shadow(Qt::black), m_opacity(1.0), m_roundness(0){} AeroButton::~AeroButton(){} void AeroButton::paintEvent(QPaintEvent * pe) { Q_UNUSED(pe); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); //test for state changes QColor button_color; if(this->isEnabled()) { m_hovered ? button_color = m_highlight : button_color = m_color; if(m_pressed) { button_color = m_highlight.darker(50); } } else { button_color = QColor(50, 50, 50); } QRect button_rect = this->geometry(); //outline painter.setPen(QPen(QBrush(Qt::black), 2.0)); QPainterPath outline; outline.addRoundedRect(0, 0, button_rect.width(), button_rect.height(), m_roundness, m_roundness); painter.setOpacity(m_opacity); painter.drawPath(outline); //gradient QLinearGradient gradient(0, 0, 0, button_rect.height()); gradient.setSpread(QGradient::ReflectSpread); gradient.setColorAt(0.0, button_color); gradient.setColorAt(0.4, m_shadow); gradient.setColorAt(0.6, m_shadow); gradient.setColorAt(1.0, button_color); QBrush brush(gradient); painter.setBrush(brush); painter.setPen(QPen(QBrush(button_color), 2.0)); //main button QPainterPath painter_path; painter_path.addRoundedRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, m_roundness, m_roundness); painter.setClipPath(painter_path); painter.setOpacity(m_opacity); painter.drawRoundedRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, m_roundness, m_roundness); //glass highlight painter.setBrush(QBrush(Qt::white)); painter.setPen(QPen(QBrush(Qt::white), 0.01)); painter.setOpacity(0.30); painter.drawRect(1, 1, button_rect.width() - 2, (button_rect.height() / 2) - 2); //text QString text = this->text(); if(!text.isNull()) { QFont font = this->font(); painter.setFont(font); painter.setPen(Qt::white); painter.setOpacity(1.0); painter.drawText(0, 0, button_rect.width(), button_rect.height(), Qt::AlignCenter, text); } //icon QIcon icon = this->icon(); if(!icon.isNull()) { QSize icon_size = this->iconSize(); QRect icon_position = this->calculateIconPosition(button_rect, icon_size); painter.setOpacity(1.0); painter.drawPixmap(icon_position, QPixmap(icon.pixmap(icon_size))); } } void AeroButton::enterEvent(QEvent * e) { m_hovered = true; this->repaint(); QPushButton::enterEvent(e); } void AeroButton::leaveEvent(QEvent * e) { m_hovered = false; this->repaint(); QPushButton::leaveEvent(e); } void AeroButton::mousePressEvent(QMouseEvent * e) { m_pressed = true; this->repaint(); QPushButton::mousePressEvent(e); } void AeroButton::mouseReleaseEvent(QMouseEvent * e) { m_pressed = false; this->repaint(); QPushButton::mouseReleaseEvent(e); } QRect AeroButton::calculateIconPosition(QRect button_rect, QSize icon_size) { int x = (button_rect.width() - icon_size.width()) / 2; int y = (button_rect.height() - icon_size.height()) / 2; int width = icon_size.width(); int height = icon_size.height(); QRect icon_position; icon_position.setX(x); icon_position.setY(y); icon_position.setWidth(width); icon_position.setHeight(height); return icon_position; } //Usage #include "AeroButton.h" AeroButton * button = new AeroButton("Test", this); button->setColor(QColor(Qt::darkRed)); button->setShadow(QColor(Qt::black)); button->setHighlight(QColor(Qt::red)); button->setRoundness(20); button->setOpacity(0.65); connect(button, SIGNAL(clicked()), myWidget, SLOT(handleClick())); |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=447