江干区住房和城乡建设局网站,linux系统如何做网站,wordpress 邮箱订阅,做网站和优化共多少钱?概述 使用Qt如何制作一个滑动开关按钮#xff0c;同类的文章和代码网上很多#xff0c;但很多都是pyqt编写的#xff0c;也有c编写的#xff0c;大家可以参考. 我这里主要是实现了一个滑动按钮#xff0c;富有滑动动画和文字#xff0c;话不多说#xff0c;上代码 自定义…概述 使用Qt如何制作一个滑动开关按钮同类的文章和代码网上很多但很多都是pyqt编写的也有c编写的大家可以参考. 我这里主要是实现了一个滑动按钮富有滑动动画和文字话不多说上代码 自定义滑动按钮 c/Qt实现 .h文件
#ifndef SwitchButtonInsideINSIDE_H
#define SwitchButtonInsideINSIDE_H#include QWidget#include customcomponent_global.hclass Slider;class CUSTOMCOMPONENT_EXPORT SwitchButtonInside : public QWidget
{Q_OBJECTpublic:explicit SwitchButtonInside(QWidget *parent nullptr);~SwitchButtonInside();/*** brief SetSize 设置按钮的尺寸* param nWidth 按钮的新宽度* param nHeight 按钮的新高度*/void SetSize(int nWidth, int nHeight);/*** brief SetActiveColor 设置按钮激活时候的颜色* param color 激活颜色*/void SetActiveColor(const QColor color);/*** brief SetInactiveColor 设置按钮未激活时候的颜色* param color 未激活颜色*/void SetInactiveColor(const QColor color);/*** brief SetSliderColor 设置滑块颜色* param color 滑块的颜色*/void SetSliderColor(const QColor color);/*** brief SetStatus 设置按钮状态* param bActive true: 激活false: 未激活*/void SetStatus(bool bActive);/*** brief GetStatus 获取按钮当前状态* return true: 激活false: 未激活*/bool GetStatus() const;/*** brief SetStatus 设置按钮显示文字* param text: 文字内容*/void SetText(const QString text);protected:void paintEvent(QPaintEvent *event) override;void mousePressEvent(QMouseEvent *event) override;void mouseReleaseEvent(QMouseEvent *event) override;void ToActive();void ToInactive();private:bool m_bActive; // 是否激活int m_nArcRadius; // 圆弧的半径int m_nRectWidth; // 矩形的宽度const short m_nMargin 2;const int m_nDuration 100; // 动画时间单位毫秒bool m_bClicked; // 能否被点击。如果动画还没结束无法进行点击/状态切换QColor m_colorActive; // 激活时的颜色QColor m_colorInactive;Slider* m_pSlider;QString m_text; // 显示文字signals:/*** brief Clicked 按钮被点击后发出的信号* param status 当前按钮状态。true为activefalse为inactive*/void Clicked(bool status);
};class Slider : public QWidget
{Q_OBJECT
public:explicit Slider(QWidget* parent nullptr);~Slider();/*** brief SetSliderColor 设置滑块颜色* param color*/void SetSliderColor(const QColor color);protected:void paintEvent(QPaintEvent* e) override;private:QColor m_sliderColor;
};#endif // SwitchButtonInsideINSIDE_H.cpp文件
#include switchbuttoninside.h
#include QPainter
#include QFont
#include QPainterPath
#include QPropertyAnimationSwitchButtonInside::SwitchButtonInside(QWidget *parent) :QWidget(parent)
{resize(72, 28); // 默认8028宽高m_pSlider new Slider(this);m_pSlider-resize(height() - m_nMargin * 2, height() - m_nMargin * 2);m_pSlider-move(m_nMargin, m_nMargin);m_bActive false; // 默认未激活m_nArcRadius std::min(width(), height()); // 默认半径m_nRectWidth width() - m_nArcRadius;m_colorActive qRgb(60, 189, 136);m_colorInactive qRgb(167, 177, 188);
}SwitchButtonInside::~SwitchButtonInside()
{
}void SwitchButtonInside::SetSize(int nWidth, int nHeight)
{resize(nWidth, nHeight);m_pSlider-resize(height() - m_nMargin * 2, height() - m_nMargin * 2);m_pSlider-move(m_nMargin, m_nMargin);m_nArcRadius std::min(width(), height());m_nRectWidth width() - m_nArcRadius;
}void SwitchButtonInside::SetActiveColor(const QColor color)
{m_colorActive color;
}void SwitchButtonInside::SetInactiveColor(const QColor color)
{m_colorInactive color;
}void SwitchButtonInside::SetSliderColor(const QColor color)
{m_pSlider-SetSliderColor(color);
}void SwitchButtonInside::SetStatus(bool bActive)
{if(m_bActive bActive) {return;}m_bActive bActive;if(m_bActive) {ToActive();} else {ToInactive();}
}bool SwitchButtonInside::GetStatus() const
{return m_bActive;
}void SwitchButtonInside::SetText(const QString text)
{m_text text;
}void SwitchButtonInside::paintEvent(QPaintEvent *)
{qDebug() [SwitchButtonInside]m_nArcRadius m_nArcRadius | m_nRectWidth m_nRectWidth | size width() , height();if (m_nArcRadius height()) {qDebug() ******* switchbutton resize ******;SetSize(width(), height());}QPainter p;p.begin(this);p.setRenderHint(QPainter::Antialiasing, true);p.setPen(Qt::NoPen);if(m_bActive) p.setBrush(QBrush(m_colorActive));else p.setBrush(QBrush(m_colorInactive));QPainterPath leftPath;leftPath.addEllipse(0, 0, m_nArcRadius, m_nArcRadius);QPainterPath middlePath;middlePath.addRect(m_nArcRadius / 2, 0, m_nRectWidth, m_nArcRadius);QPainterPath rightPath;rightPath.addEllipse(m_nRectWidth, 0, m_nArcRadius, m_nArcRadius);QPainterPath path leftPath middlePath rightPath;p.drawPath(path);QPen pen;pen.setColor(Qt::white);p.setPen(pen);QFont ft;ft.setPointSize(9);p.setFont(ft);if (m_bActive) {p.drawText(QRect(0, 0, m_nRectWidth,m_nArcRadius), Qt::AlignCenter, m_text);} else {p.drawText(QRect(m_nArcRadius, 0,m_nRectWidth, m_nArcRadius), Qt::AlignCenter, m_text);}p.end();
}void SwitchButtonInside::mousePressEvent(QMouseEvent *event)
{QWidget::mousePressEvent(event);
}void SwitchButtonInside::mouseReleaseEvent(QMouseEvent *event)
{emit Clicked(!m_bActive);QWidget::mouseReleaseEvent(event);
}void SwitchButtonInside::ToActive()
{QPropertyAnimation* pAnimation new QPropertyAnimation(m_pSlider, geometry);pAnimation-setDuration(m_nDuration);pAnimation-setStartValue(m_pSlider-rect());pAnimation-setEndValue(QRect(width() - m_pSlider-width() - m_nMargin,m_nMargin,m_pSlider-width(),m_pSlider-height()));connect(pAnimation, QPropertyAnimation::valueChanged, this, [](const QVariant value){Q_UNUSED(value)update();});pAnimation-start(QAbstractAnimation::DeleteWhenStopped);
}void SwitchButtonInside::ToInactive()
{QPropertyAnimation* pAnimation new QPropertyAnimation(m_pSlider, geometry);pAnimation-setDuration(m_nDuration);pAnimation-setStartValue(QRect(m_pSlider-x(),m_pSlider-y(),m_pSlider-width(),m_pSlider-height()));pAnimation-setEndValue(QRect(m_nMargin,m_nMargin,m_pSlider-width(),m_pSlider-height()));connect(pAnimation, QPropertyAnimation::valueChanged, this, [](const QVariant value){Q_UNUSED(value)update();});pAnimation-start(QAbstractAnimation::DeleteWhenStopped);
}///
/// Slider 滑块类 //
//Slider::Slider(QWidget *parent) : QWidget(parent)
{m_sliderColor Qt::white;resize(56, 56);
}Slider::~Slider()
{}void Slider::SetSliderColor(const QColor color)
{m_sliderColor color;update();
}void Slider::paintEvent(QPaintEvent *e)
{QPainter p(this);p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);p.fillRect(rect(), Qt::transparent);p.setBrush(m_sliderColor);p.setPen(Qt::NoPen);p.drawRoundedRect(rect(), width() / 2, height() / 2);QWidget::paintEvent(e);
}
文章转载自: http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.kqpq.cn.gov.cn.kqpq.cn http://www.morning.dkqr.cn.gov.cn.dkqr.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.mtzyr.cn.gov.cn.mtzyr.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.dkslm.cn.gov.cn.dkslm.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.qrmry.cn.gov.cn.qrmry.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn