SwitchButton是滑动按钮组件,可开启/关闭滑动动画效果,可指定大小,可指定背景色和滑块颜色及文字颜色,可设定文字及字体。
LineSwitch是形条组件,其包含了图标+文字条+SwitchButton,可指定大小,可指定背景渐变色和文字颜色,可设定文字及字体。
源代码如下,需要自卑item0.png 和 item1.png两个图标。
SwitchButton.h
#ifndef SWITCHBUTTON_H
#define SWITCHBUTTON_H
#include <QWidget>
#include <QTimer>
#include <QColor>
class SwitchButton : public QWidget
{
Q_OBJECT
public:
explicit SwitchButton(QWidget *parent = 0);
~SwitchButton(){}
signals:
void statusChanged(qint16 id,bool checked);
public slots:
private slots:
void updateValue();
private:
void drawBackGround(QPainter *painter);
void drawSlider(QPainter *painter);
protected:
void paintEvent(QPaintEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);
void resizeEvent(QResizeEvent *event);
private:
qint16 m_ID;
int m_space; //distance between slider and boundary
int m_radius; //fillet angle
bool m_checked; //switch on/off
bool m_showText; //display text or not
bool m_showCircle; //display circle or not
bool m_animation; //use animation or not
QColor m_bgColorOn; //background color when switch on
QColor m_bgColorOff; //background color when switch off
QColor m_sliderColorOn; //slider color when switch on
QColor m_sliderColorOff; //slider color when switch off
QColor m_textColor; //text color
QString m_textOn; //text when switch on
QString m_textOff; //text when switch off
QTimer *m_timer; //animation timer
int m_step; //animation step x pixel
int m_startX; //slider start X pos
int m_endX; //slider end X pos
public:
qint16 getID();
int space() const;
int radius() const;
bool checked() const;
bool showText() const;
bool showCircel() const;
bool animation() const;
QColor bgColorOn() const;
QColor bgColorOff() const;
QColor sliderColorOn() const;
QColor sliderColorOff() const;
QColor textColor() const;
QString textOn() const;
QString textOff() const;
int step() const;
int startX() const;
int endX() const;
public Q_SLOTS:
void setID(qint16 id);
void setSpace(int space);
void setRadius(int radius);
void setChecked(bool checked);
void setShowText(bool show);
void setShowCircle(bool show);
void setAnimation(bool ok);
void setBgColorOn(const QColor &color);
void setBgColorOff(const QColor &color);
void setSliderColorOn(const QColor &color);
void setSliderColorOff(const QColor &color);
void setTextColor(const QColor &color);
void setTextOn(const QString &text);
void setTextOff(const QString &text);
};
#endif
SwitchButton.cpp
#pragma execution_character_set("utf-8")
#include "SwitchButton.h"
#include <QPainter>
#include <QPainterPath>
#include <QDebug>
SwitchButton::SwitchButton(QWidget *parent) : QWidget(parent)
{
m_ID=0;
m_space = 2;
m_radius = 4;
m_checked = false;
m_showText = true;
m_showText = false;
m_animation = true;
//m_bgColorOn = QColor(97,81,233); //紫蓝
//m_bgColorOn = QColor(21,137,200); //淡蓝
m_bgColorOn = QColor(0, 204, 106); //翠绿
m_bgColorOff = QColor(111, 122, 126);
m_sliderColorOn = QColor(255, 255, 255);
m_sliderColorOff = QColor(255, 255, 255);
m_textColor = QColor(255, 255, 255);
m_textOn = "打开";
m_textOff = "关闭";
m_step = 0;
m_startX = 0;
m_endX = 0;
m_timer = new QTimer(this);
m_timer->setInterval(30);
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateValue()));
}
void SwitchButton::drawBackGround(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QColor bgColor = m_checked ? m_bgColorOn : m_bgColorOff;
// if (isEnabled()) {
// bgColor.setAlpha(60);
// }
painter->setBrush(bgColor);
QRect rect(0, 0, width(), height());
int side = qMin(width(), height());
//左侧半圆 Left semicircle
QPainterPath path1;
path1.addEllipse(rect.x(), rect.y(), side, side);
//右侧半圆 Right semicircle
QPainterPath path2;
path2.addEllipse(rect.width() - side, rect.y(), side, side);
//中间的矩形 Middle rectangle
QPainterPath path3;
path3.addRect(rect.x() + side / 2, rect.y(), rect.width() - side, height());
QPainterPath path = path1 + path2 + path3;
painter->drawPath(path);
QFont font;
font.setPointSize(height()/2);
font.setFamily("Microsoft YaHei");
font.setLetterSpacing(QFont::AbsoluteSpacing,0);// 设置字符间距 Set character spacing. default 0
painter->setFont(font);
// QFontDatabase::addApplicationFont("DEMO_FONT.TTF"); use self font
// painter->setFont(QFont("DEMO_FONT", 34));
//slider R
int sliderWidth = qMin(height(), width()) - m_space * 2 - 5;
if (m_checked){
QRect textRect(0, 0, width() - sliderWidth, height()*0.95);
painter->setPen(QPen(m_textColor));
painter->drawText(textRect, Qt::AlignCenter, m_textOn);
} else {
QRect textRect(sliderWidth, 0, width() - sliderWidth, height()*0.95);
painter->setPen(QPen(m_textColor));
painter->drawText(textRect, Qt::AlignCenter, m_textOff);
}
painter->restore();
}
void SwitchButton::drawSlider(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QColor color = m_checked ? m_sliderColorOn : m_sliderColorOff;
painter->setBrush(QBrush(color));
int sliderWidth = qMin(width(), height()) - m_space * 2;
QRect rect(m_space + m_startX, m_space, sliderWidth, sliderWidth);
painter->drawEllipse(rect);
painter->restore();
}
void SwitchButton::paintEvent(QPaintEvent *ev)
{
//启用反锯齿 Enable anti aliasing
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
drawBackGround(&painter);
drawSlider(&painter);
}
v