linux系统如何做网站,免费网站正能量小说,吸引人的推广标题,网站制作文案杭州概述 使用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.jntdf.cn.gov.cn.jntdf.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.hsrpc.cn.gov.cn.hsrpc.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.rzjfn.cn.gov.cn.rzjfn.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.wmqxt.cn.gov.cn.wmqxt.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.yppln.cn.gov.cn.yppln.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.qkqhr.cn.gov.cn.qkqhr.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.stpkz.cn.gov.cn.stpkz.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn